library(readr)
b_data <- read_csv("/Users/raj/Desktop/Billionaires Statistics Dataset.csv")
## Rows: 197 Columns: 22
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): category, personName, country, city, source, industries, countryOf...
## dbl  (8): rank, age, finalWorth, birthYear, birthMonth, birthDay, cpi_countr...
## lgl  (1): selfMade
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
attach(b_data)
str(b_data)
## spc_tbl_ [197 × 22] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ rank                : num [1:197] 1 2 3 4 5 6 7 8 9 10 ...
##  $ age                 : num [1:197] 74 51 59 78 92 67 81 83 65 67 ...
##  $ finalWorth          : num [1:197] 211000 180000 114000 107000 106000 104000 94500 93000 83400 80700 ...
##  $ birthYear           : num [1:197] 1949 1971 1964 1944 1930 ...
##  $ birthMonth          : num [1:197] 3 6 1 8 8 10 2 1 4 3 ...
##  $ birthDay            : num [1:197] 5 28 12 17 30 28 14 28 19 24 ...
##  $ category            : chr [1:197] "Fashion & Retail" "Automotive" "Technology" "Technology" ...
##  $ personName          : chr [1:197] "Bernard Arnault & family" "Elon Musk" "Jeff Bezos" "Larry Ellison" ...
##  $ country             : chr [1:197] "France" "United States" "United States" "United States" ...
##  $ city                : chr [1:197] "Paris" "Austin" "Medina" "Lanai" ...
##  $ source              : chr [1:197] "LVMH" "Tesla, SpaceX" "Amazon" "Oracle" ...
##  $ industries          : chr [1:197] "Fashion & Retail" "Automotive" "Technology" "Technology" ...
##  $ countryOfCitizenship: chr [1:197] "France" "United States" "United States" "United States" ...
##  $ organization        : chr [1:197] "LVMH Moët Hennessy Louis Vuitton" "Tesla" "Amazon" "Oracle" ...
##  $ selfMade            : logi [1:197] FALSE TRUE TRUE TRUE TRUE TRUE ...
##  $ gender              : chr [1:197] "M" "M" "M" "M" ...
##  $ birthDate           : chr [1:197] "3/5/49 0:00" "6/28/71 0:00" "1/12/64 0:00" "8/17/44 0:00" ...
##  $ state               : chr [1:197] NA "Texas" "Washington" "Hawaii" ...
##  $ residenceStateRegion: chr [1:197] NA "South" "West" "West" ...
##  $ cpi_country         : num [1:197] 110 117 117 117 117 ...
##  $ cpi_change_country  : num [1:197] 1.1 7.5 7.5 7.5 7.5 7.5 7.5 3.6 7.7 7.5 ...
##  $ gdp_country         : chr [1:197] "$2,715,518,274,227.00" "$21,427,700,000,000.00" "$21,427,700,000,000.00" "$21,427,700,000,000.00" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   rank = col_double(),
##   ..   age = col_double(),
##   ..   finalWorth = col_double(),
##   ..   birthYear = col_double(),
##   ..   birthMonth = col_double(),
##   ..   birthDay = col_double(),
##   ..   category = col_character(),
##   ..   personName = col_character(),
##   ..   country = col_character(),
##   ..   city = col_character(),
##   ..   source = col_character(),
##   ..   industries = col_character(),
##   ..   countryOfCitizenship = col_character(),
##   ..   organization = col_character(),
##   ..   selfMade = col_logical(),
##   ..   gender = col_character(),
##   ..   birthDate = col_character(),
##   ..   state = col_character(),
##   ..   residenceStateRegion = col_character(),
##   ..   cpi_country = col_double(),
##   ..   cpi_change_country = col_double(),
##   ..   gdp_country = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
excluded_columns <- c(-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,-21,-22)
#cor(b_data[-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,-21,-22])
result <- cor(b_data[,excluded_columns])
excluded_columns
##  [1]  -4  -5  -6  -7  -8  -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22
result
##                   rank         age  finalWorth
## rank        1.00000000 -0.07913784 -0.70553965
## age        -0.07913784  1.00000000  0.02832739
## finalWorth -0.70553965  0.02832739  1.00000000
b_data_pca <- prcomp(b_data[,excluded_columns],scale=TRUE)
b_data_pca
## Standard deviations (1, .., p=3):
## [1] 1.3090613 0.9968619 0.5409481
## 
## Rotation (n x k) = (3 x 3):
##                   PC1         PC2       PC3
## rank        0.7050171  0.03888488 0.7081235
## age        -0.1060159  0.99305470 0.0510196
## finalWorth -0.7012215 -0.11104201 0.7042430
summary(b_data_pca)
## Importance of components:
##                           PC1    PC2     PC3
## Standard deviation     1.3091 0.9969 0.54095
## Proportion of Variance 0.5712 0.3312 0.09754
## Cumulative Proportion  0.5712 0.9025 1.00000
# sample scores stored 
(eigen_b_data <- b_data_pca$sdev^2)
## [1] 1.7136414 0.9937337 0.2926249
names(eigen_b_data) <- paste("PC",1:3,sep="")
eigen_b_data
##       PC1       PC2       PC3 
## 1.7136414 0.9937337 0.2926249
sumlambdas <- sum(eigen_b_data)
sumlambdas
## [1] 3
propvar <- eigen_b_data/sumlambdas
propvar
##        PC1        PC2        PC3 
## 0.57121381 0.33124456 0.09754163
cumvar_b_data <- cumsum(propvar)
cumvar_b_data
##       PC1       PC2       PC3 
## 0.5712138 0.9024584 1.0000000
matlambdas <- rbind(eigen_b_data,propvar,cumvar_b_data)
rownames(matlambdas) <- c("Eigenvalues","Prop. variance","Cum. prop. variance")
round(matlambdas,4)
##                        PC1    PC2    PC3
## Eigenvalues         1.7136 0.9937 0.2926
## Prop. variance      0.5712 0.3312 0.0975
## Cum. prop. variance 0.5712 0.9025 1.0000
summary(b_data_pca)
## Importance of components:
##                           PC1    PC2     PC3
## Standard deviation     1.3091 0.9969 0.54095
## Proportion of Variance 0.5712 0.3312 0.09754
## Cumulative Proportion  0.5712 0.9025 1.00000
b_data_pca$rotation
##                   PC1         PC2       PC3
## rank        0.7050171  0.03888488 0.7081235
## age        -0.1060159  0.99305470 0.0510196
## finalWorth -0.7012215 -0.11104201 0.7042430
print(b_data_pca)
## Standard deviations (1, .., p=3):
## [1] 1.3090613 0.9968619 0.5409481
## 
## Rotation (n x k) = (3 x 3):
##                   PC1         PC2       PC3
## rank        0.7050171  0.03888488 0.7081235
## age        -0.1060159  0.99305470 0.0510196
## finalWorth -0.7012215 -0.11104201 0.7042430
## Sample scores stored in b_data_pca$x
b_data_pca$x
##                 PC1         PC2         PC3
##   [1,] -6.018762029 -0.40221757  3.59191907
##   [2,] -5.028184548 -1.94155766  2.71486049
##   [3,] -3.373832285 -1.09131018  1.04572055
##   [4,] -3.327800487  0.31487183  0.94731796
##   [5,] -3.397990109  1.33418580  0.98587088
##   [6,] -3.140640833 -0.46865024  0.85329324
##   [7,] -2.991383577  0.58541427  0.67145370
##   [8,] -2.955827138  0.73716061  0.65236480
##   [9,] -2.556421699 -0.52733082  0.34879229
##  [10,] -2.489884525 -0.37067853  0.29858916
##  [11,] -2.487890549 -0.22424698  0.31320735
##  [12,] -2.295032079 -1.59513894  0.22111845
##  [13,] -2.519924539  1.09461044  0.32196766
##  [14,] -2.180073486 -1.65316685  0.15913931
##  [15,] -2.108224408 -0.24289654  0.03480820
##  [16,] -1.770885226 -2.40152955 -0.15787072
##  [17,] -1.998254628  1.17214065 -0.10309380
##  [18,] -1.789370431 -0.78448587 -0.20361828
##  [19,] -1.867910453  0.23223606 -0.13196492
##  [20,] -1.855572101  0.52769120 -0.13582900
##  [21,] -1.781350867  0.16971101 -0.16542268
##  [22,] -1.647776087 -0.39994851 -0.24248568
##  [23,] -1.470303199 -0.88896378 -0.36768261
##  [24,] -1.398602569 -0.73149382 -0.42307143
##  [25,] -1.525494151  1.08946142 -0.37208548
##  [26,] -1.154732220 -2.24296302 -0.53358463
##  [27,] -1.428616386  0.95487756 -0.41185923
##  [28,] -1.367233935  1.18440633 -0.46093207
##  [29,] -1.321376780  1.11670557 -0.47822609
##  [30,] -1.095025704 -0.83842471 -0.57157864
##  [31,] -1.260643206  0.97639809 -0.48169990
##  [32,] -1.291589013  1.26626868 -0.46680739
##  [33,] -1.313392145  1.77612589 -0.42380878
##  [34,] -0.898714642 -1.32826602 -0.64155270
##  [35,] -1.079813780  0.55779794 -0.54017227
##  [36,] -0.901875390 -1.10895799 -0.62580424
##  [37,] -0.683848518 -2.84559790 -0.69822262
##  [38,] -0.823657472 -1.10038095 -0.63021697
##  [39,] -0.945429260  0.35292126 -0.56413958
##  [40,] -0.850589131 -0.29656484 -0.59825437
##  [41,] -0.895005134  0.35836674 -0.56535284
##  [42,] -0.879532230  0.21343144 -0.57279910
##  [43,] -0.921962989  0.94110043 -0.52122395
##  [44,] -0.891017182  0.65122983 -0.53611647
##  [45,] -0.569817245 -1.87969241 -0.66763924
##  [46,] -0.885009058  1.31160689 -0.50442819
##  [47,] -0.929433793  1.89284433 -0.46747124
##  [48,] -0.651483086 -0.34888590 -0.59645912
##  [49,] -0.783581785  1.10278099 -0.52001032
##  [50,] -0.701641564  0.52494488 -0.54521627
##  [51,] -0.555228001 -0.63222360 -0.60280011
##  [52,] -0.602225732  0.02229915 -0.56730573
##  [53,] -0.442920993 -1.28021345 -0.62974297
##  [54,] -0.567291973  0.02529165 -0.55296187
##  [55,] -0.591062884  0.60979973 -0.53674773
##  [56,] -0.552968378  0.39348420 -0.53815234
##  [57,] -0.566447305  0.68485105 -0.51608789
##  [58,] -0.342677957 -1.27068806 -0.60684759
##  [59,] -0.353566424 -0.90521825 -0.59143143
##  [60,] -0.305153738 -1.19359259 -0.59915201
##  [61,] -0.519780411  0.98193313 -0.48028622
##  [62,] -0.339839313 -0.46469711 -0.55535543
##  [63,] -0.291426627 -0.75307146 -0.56307601
##  [64,] -0.446743414  0.84230424 -0.47140240
##  [65,] -0.426694807  0.84420932 -0.46682332
##  [66,] -0.449904162  1.06161226 -0.45565394
##  [67,] -0.319559986  0.12170766 -0.48452505
##  [68,] -0.384612348  0.92075688 -0.43899155
##  [69,] -0.243362241 -0.23722925 -0.49138969
##  [70,] -0.231050086 -0.16285653 -0.48308748
##  [71,] -0.107854610 -1.17631619 -0.52544651
##  [72,] -0.154852340 -0.52179344 -0.48995212
##  [73,] -0.201271051 -0.08698755 -0.46761335
##  [74,]  0.073492712 -2.33049217 -0.56868649
##  [75,] -0.050290516 -1.17100979 -0.50911642
##  [76,] -0.084993555 -0.58950258 -0.45720898
##  [77,] -0.302201956  1.58561431 -0.33575033
##  [78,] -0.201628084  0.64353487 -0.38415101
##  [79,] -0.019710474 -0.80405210 -0.43649117
##  [80,] -0.113126915  0.28527654 -0.37865801
##  [81,] -0.185915729  1.15679341 -0.32940139
##  [82,]  0.019816452 -0.50683095 -0.41823284
##  [83,] -0.047808905  0.36550358 -0.37416192
##  [84,]  0.075386570 -0.64795609 -0.41652094
##  [85,]  0.106332377 -0.93782669 -0.43141346
##  [86,]  0.002001273  0.29588935 -0.34599785
##  [87,]  0.118048049 -0.79112538 -0.40184479
##  [88,]  0.147818351 -0.78895054 -0.38231522
##  [89,]  0.010556196  0.66189869 -0.30068072
##  [90,]  0.103393617 -0.20771310 -0.34535826
##  [91,]  0.064711358  0.15462514 -0.32674262
##  [92,]  0.184158334 -0.49350378 -0.33614214
##  [93,]  0.119105972  0.30554544 -0.29060865
##  [94,]  0.182991561 -0.12776421 -0.30577549
##  [95,]  0.020526075  1.39405642 -0.22758979
##  [96,]  0.128836399  0.37950934 -0.27971359
##  [97,]  0.036808718  1.61390397 -0.18194035
##  [98,]  0.090963880  1.10663043 -0.20800225
##  [99,]  0.053687845  1.76142293 -0.15755738
## [100,]  0.246520118  0.16944856 -0.23748000
## [101,]  0.248514094  0.31588011 -0.22286181
## [102,]  0.380033774 -0.91606992 -0.28615500
## [103,]  0.278284397  0.31805495 -0.20333225
## [104,]  0.269960193  0.53654532 -0.18239808
## [105,]  0.331851807 -0.04319588 -0.21218311
## [106,]  0.454459531 -0.91063283 -0.23733109
## [107,]  0.376507261 -0.03993362 -0.18288877
## [108,]  0.267609185  1.12063619 -0.11355393
## [109,]  0.321764347  0.61336264 -0.13961583
## [110,]  0.445769562 -0.03531418 -0.12887916
## [111,]  0.630856651 -1.62851504 -0.20102321
## [112,]  0.470385141  0.03973714 -0.10821932
## [113,]  0.578107713 -0.82878723 -0.14313208
## [114,]  0.389272123  1.12974437 -0.03802853
## [115,]  0.404745027  0.98480907 -0.04547479
## [116,]  0.455142957  0.76917213 -0.03452176
## [117,]  0.547392625  0.04558306 -0.06198827
## [118,]  0.624169391 -0.53307071 -0.08200852
## [119,]  0.646790994 -0.60445094 -0.07596686
## [120,]  0.584311628  0.12131297 -0.02897080
## [121,]  0.398049034  2.00655926  0.07759534
## [122,]  0.722392255 -0.89105928 -0.06156504
## [123,]  0.645027737 -0.16638279 -0.02433375
## [124,]  0.455595663  1.86447739  0.10203627
## [125,]  0.671628560 -0.01859406  0.01499970
## [126,]  0.763878228 -0.74218313 -0.01246680
## [127,]  0.477629514  1.93911988  0.12528896
## [128,]  0.631183045  0.78181233  0.08524846
## [129,]  0.646655948  0.63687704  0.07780221
## [130,]  0.871013049 -1.46468479 -0.03016853
## [131,]  0.662947323  0.93041872  0.11939621
## [132,]  0.662947323  0.93041872  0.11939621
## [133,]  0.732575389  0.27820988  0.08588805
## [134,]  0.857168356 -0.51648966  0.07941368
## [135,]  0.639959955  1.65862723  0.20087233
## [136,]  0.858574580 -0.22403540  0.11124291
## [137,]  0.773473611  0.57310874  0.15219733
## [138,]  0.937345321 -0.65625762  0.10584085
## [139,]  0.965130380 -0.72682019  0.10669679
## [140,]  1.034170693 -1.23300632  0.09039967
## [141,]  0.887178110  0.14387901  0.16113912
## [142,]  0.891157330  0.36304797  0.19443092
## [143,]  1.006616354 -0.57794405  0.15579503
## [144,]  0.714625163  2.32225816  0.31189211
## [145,]  0.863611722  1.09180438  0.25577085
## [146,]  1.218900750 -2.09568476  0.10171796
## [147,]  0.948124939  0.44068296  0.23202747
## [148,]  0.948124939  0.44068296  0.23202747
## [149,]  1.095927262 -0.57141953  0.21438371
## [150,]  1.165555327 -1.22362837  0.18087555
## [151,]  1.007069060  0.51736121  0.29235306
## [152,]  1.045751319  0.15502297  0.27373742
## [153,]  1.099906481 -0.35225058  0.24767551
## [154,]  1.138588740 -0.71458882  0.22905987
## [155,]  0.953135885  1.53544032  0.38872169
## [156,]  1.076919113  0.37595793  0.32915163
## [157,]  1.212418012 -0.63682315  0.29915024
## [158,]  1.266573174 -1.14409669  0.27308834
## [159,]  1.046195293  1.17663409  0.41435088
## [160,]  1.030722390  1.32156939  0.42179713
## [161,]  1.169978521  0.01715170  0.35478082
## [162,]  1.010904502  1.90416401  0.48346936
## [163,]  1.118627074  1.03563964  0.44855660
## [164,]  1.180518688  0.45589844  0.41877157
## [165,]  1.331490491 -0.70181793  0.38132392
## [166,]  1.470746622 -2.00623561  0.31430760
## [167,]  1.424327912 -1.57142972  0.33664637
## [168,]  1.272990344  0.24311493  0.46161179
## [169,]  1.496759692 -1.71242417  0.37085209
## [170,]  1.365240013 -0.48047414  0.43414528
## [171,]  1.195038075  1.11381414  0.51605412
## [172,]  1.334294206 -0.19060354  0.44903780
## [173,]  1.256929689  0.53407295  0.48626908
## [174,]  1.512232596 -1.85735947  0.36340583
## [175,]  1.380712916 -0.62540944  0.42669902
## [176,]  1.256929689  0.53407295  0.48626908
## [177,]  1.597768807 -1.70658663  0.46712031
## [178,]  1.303783641  1.04718404  0.60859920
## [179,]  1.473985580 -0.54710424  0.52669037
## [180,]  1.312329833  1.33949923  0.65797176
## [181,]  1.412315953  0.54344251  0.62678213
## [182,]  1.427201104  0.54452993  0.63654691
## [183,]  1.380782394  0.97933583  0.65888568
## [184,]  1.605139494 -1.12222600  0.55091495
## [185,]  1.589666591 -0.97729070  0.55836120
## [186,]  1.574193687 -0.83235540  0.56580746
## [187,]  1.419464653  0.61699758  0.64027004
## [188,]  1.588704342 -0.24813384  0.66714544
## [189,]  1.619650149 -0.53800444  0.65225292
## [190,]  1.666068859 -0.97281033  0.62991415
## [191,]  1.488130469  0.69394560  0.71554612
## [192,]  1.580967890 -0.17566619  0.67086857
## [193,]  1.598648024  0.26294148  0.75240266
## [194,]  1.598648024  0.26294148  0.75240266
## [195,]  1.621857380  0.04553853  0.74123327
## [196,]  1.691485445 -0.60667031  0.70772511
## [197,]  1.660539638 -0.31679971  0.72261763
# Identifying the scores by their selfMade status
b_datatyp_pca <- cbind(data.frame(selfMade),b_data_pca$x)


print(b_datatyp_pca)
##     selfMade          PC1         PC2         PC3
## 1      FALSE -6.018762029 -0.40221757  3.59191907
## 2       TRUE -5.028184548 -1.94155766  2.71486049
## 3       TRUE -3.373832285 -1.09131018  1.04572055
## 4       TRUE -3.327800487  0.31487183  0.94731796
## 5       TRUE -3.397990109  1.33418580  0.98587088
## 6       TRUE -3.140640833 -0.46865024  0.85329324
## 7       TRUE -2.991383577  0.58541427  0.67145370
## 8       TRUE -2.955827138  0.73716061  0.65236480
## 9      FALSE -2.556421699 -0.52733082  0.34879229
## 10      TRUE -2.489884525 -0.37067853  0.29858916
## 11     FALSE -2.487890549 -0.22424698  0.31320735
## 12      TRUE -2.295032079 -1.59513894  0.22111845
## 13      TRUE -2.519924539  1.09461044  0.32196766
## 14      TRUE -2.180073486 -1.65316685  0.15913931
## 15      TRUE -2.108224408 -0.24289654  0.03480820
## 16      TRUE -1.770885226 -2.40152955 -0.15787072
## 17     FALSE -1.998254628  1.17214065 -0.10309380
## 18     FALSE -1.789370431 -0.78448587 -0.20361828
## 19     FALSE -1.867910453  0.23223606 -0.13196492
## 20     FALSE -1.855572101  0.52769120 -0.13582900
## 21     FALSE -1.781350867  0.16971101 -0.16542268
## 22     FALSE -1.647776087 -0.39994851 -0.24248568
## 23      TRUE -1.470303199 -0.88896378 -0.36768261
## 24      TRUE -1.398602569 -0.73149382 -0.42307143
## 25      TRUE -1.525494151  1.08946142 -0.37208548
## 26      TRUE -1.154732220 -2.24296302 -0.53358463
## 27     FALSE -1.428616386  0.95487756 -0.41185923
## 28      TRUE -1.367233935  1.18440633 -0.46093207
## 29     FALSE -1.321376780  1.11670557 -0.47822609
## 30     FALSE -1.095025704 -0.83842471 -0.57157864
## 31     FALSE -1.260643206  0.97639809 -0.48169990
## 32     FALSE -1.291589013  1.26626868 -0.46680739
## 33      TRUE -1.313392145  1.77612589 -0.42380878
## 34      TRUE -0.898714642 -1.32826602 -0.64155270
## 35     FALSE -1.079813780  0.55779794 -0.54017227
## 36      TRUE -0.901875390 -1.10895799 -0.62580424
## 37     FALSE -0.683848518 -2.84559790 -0.69822262
## 38      TRUE -0.823657472 -1.10038095 -0.63021697
## 39      TRUE -0.945429260  0.35292126 -0.56413958
## 40      TRUE -0.850589131 -0.29656484 -0.59825437
## 41     FALSE -0.895005134  0.35836674 -0.56535284
## 42     FALSE -0.879532230  0.21343144 -0.57279910
## 43      TRUE -0.921962989  0.94110043 -0.52122395
## 44      TRUE -0.891017182  0.65122983 -0.53611647
## 45      TRUE -0.569817245 -1.87969241 -0.66763924
## 46      TRUE -0.885009058  1.31160689 -0.50442819
## 47      TRUE -0.929433793  1.89284433 -0.46747124
## 48      TRUE -0.651483086 -0.34888590 -0.59645912
## 49      TRUE -0.783581785  1.10278099 -0.52001032
## 50      TRUE -0.701641564  0.52494488 -0.54521627
## 51     FALSE -0.555228001 -0.63222360 -0.60280011
## 52     FALSE -0.602225732  0.02229915 -0.56730573
## 53      TRUE -0.442920993 -1.28021345 -0.62974297
## 54     FALSE -0.567291973  0.02529165 -0.55296187
## 55      TRUE -0.591062884  0.60979973 -0.53674773
## 56      TRUE -0.552968378  0.39348420 -0.53815234
## 57      TRUE -0.566447305  0.68485105 -0.51608789
## 58      TRUE -0.342677957 -1.27068806 -0.60684759
## 59     FALSE -0.353566424 -0.90521825 -0.59143143
## 60     FALSE -0.305153738 -1.19359259 -0.59915201
## 61     FALSE -0.519780411  0.98193313 -0.48028622
## 62      TRUE -0.339839313 -0.46469711 -0.55535543
## 63      TRUE -0.291426627 -0.75307146 -0.56307601
## 64      TRUE -0.446743414  0.84230424 -0.47140240
## 65     FALSE -0.426694807  0.84420932 -0.46682332
## 66     FALSE -0.449904162  1.06161226 -0.45565394
## 67      TRUE -0.319559986  0.12170766 -0.48452505
## 68     FALSE -0.384612348  0.92075688 -0.43899155
## 69      TRUE -0.243362241 -0.23722925 -0.49138969
## 70      TRUE -0.231050086 -0.16285653 -0.48308748
## 71     FALSE -0.107854610 -1.17631619 -0.52544651
## 72     FALSE -0.154852340 -0.52179344 -0.48995212
## 73      TRUE -0.201271051 -0.08698755 -0.46761335
## 74     FALSE  0.073492712 -2.33049217 -0.56868649
## 75      TRUE -0.050290516 -1.17100979 -0.50911642
## 76      TRUE -0.084993555 -0.58950258 -0.45720898
## 77     FALSE -0.302201956  1.58561431 -0.33575033
## 78      TRUE -0.201628084  0.64353487 -0.38415101
## 79      TRUE -0.019710474 -0.80405210 -0.43649117
## 80      TRUE -0.113126915  0.28527654 -0.37865801
## 81      TRUE -0.185915729  1.15679341 -0.32940139
## 82      TRUE  0.019816452 -0.50683095 -0.41823284
## 83      TRUE -0.047808905  0.36550358 -0.37416192
## 84      TRUE  0.075386570 -0.64795609 -0.41652094
## 85      TRUE  0.106332377 -0.93782669 -0.43141346
## 86     FALSE  0.002001273  0.29588935 -0.34599785
## 87      TRUE  0.118048049 -0.79112538 -0.40184479
## 88      TRUE  0.147818351 -0.78895054 -0.38231522
## 89      TRUE  0.010556196  0.66189869 -0.30068072
## 90      TRUE  0.103393617 -0.20771310 -0.34535826
## 91      TRUE  0.064711358  0.15462514 -0.32674262
## 92      TRUE  0.184158334 -0.49350378 -0.33614214
## 93     FALSE  0.119105972  0.30554544 -0.29060865
## 94      TRUE  0.182991561 -0.12776421 -0.30577549
## 95      TRUE  0.020526075  1.39405642 -0.22758979
## 96     FALSE  0.128836399  0.37950934 -0.27971359
## 97      TRUE  0.036808718  1.61390397 -0.18194035
## 98      TRUE  0.090963880  1.10663043 -0.20800225
## 99     FALSE  0.053687845  1.76142293 -0.15755738
## 100    FALSE  0.246520118  0.16944856 -0.23748000
## 101    FALSE  0.248514094  0.31588011 -0.22286181
## 102    FALSE  0.380033774 -0.91606992 -0.28615500
## 103     TRUE  0.278284397  0.31805495 -0.20333225
## 104    FALSE  0.269960193  0.53654532 -0.18239808
## 105     TRUE  0.331851807 -0.04319588 -0.21218311
## 106     TRUE  0.454459531 -0.91063283 -0.23733109
## 107     TRUE  0.376507261 -0.03993362 -0.18288877
## 108     TRUE  0.267609185  1.12063619 -0.11355393
## 109    FALSE  0.321764347  0.61336264 -0.13961583
## 110     TRUE  0.445769562 -0.03531418 -0.12887916
## 111     TRUE  0.630856651 -1.62851504 -0.20102321
## 112     TRUE  0.470385141  0.03973714 -0.10821932
## 113     TRUE  0.578107713 -0.82878723 -0.14313208
## 114    FALSE  0.389272123  1.12974437 -0.03802853
## 115     TRUE  0.404745027  0.98480907 -0.04547479
## 116     TRUE  0.455142957  0.76917213 -0.03452176
## 117    FALSE  0.547392625  0.04558306 -0.06198827
## 118     TRUE  0.624169391 -0.53307071 -0.08200852
## 119     TRUE  0.646790994 -0.60445094 -0.07596686
## 120     TRUE  0.584311628  0.12131297 -0.02897080
## 121     TRUE  0.398049034  2.00655926  0.07759534
## 122    FALSE  0.722392255 -0.89105928 -0.06156504
## 123     TRUE  0.645027737 -0.16638279 -0.02433375
## 124    FALSE  0.455595663  1.86447739  0.10203627
## 125    FALSE  0.671628560 -0.01859406  0.01499970
## 126     TRUE  0.763878228 -0.74218313 -0.01246680
## 127     TRUE  0.477629514  1.93911988  0.12528896
## 128     TRUE  0.631183045  0.78181233  0.08524846
## 129     TRUE  0.646655948  0.63687704  0.07780221
## 130     TRUE  0.871013049 -1.46468479 -0.03016853
## 131     TRUE  0.662947323  0.93041872  0.11939621
## 132    FALSE  0.662947323  0.93041872  0.11939621
## 133     TRUE  0.732575389  0.27820988  0.08588805
## 134     TRUE  0.857168356 -0.51648966  0.07941368
## 135     TRUE  0.639959955  1.65862723  0.20087233
## 136     TRUE  0.858574580 -0.22403540  0.11124291
## 137     TRUE  0.773473611  0.57310874  0.15219733
## 138     TRUE  0.937345321 -0.65625762  0.10584085
## 139     TRUE  0.965130380 -0.72682019  0.10669679
## 140     TRUE  1.034170693 -1.23300632  0.09039967
## 141     TRUE  0.887178110  0.14387901  0.16113912
## 142     TRUE  0.891157330  0.36304797  0.19443092
## 143    FALSE  1.006616354 -0.57794405  0.15579503
## 144     TRUE  0.714625163  2.32225816  0.31189211
## 145     TRUE  0.863611722  1.09180438  0.25577085
## 146     TRUE  1.218900750 -2.09568476  0.10171796
## 147     TRUE  0.948124939  0.44068296  0.23202747
## 148     TRUE  0.948124939  0.44068296  0.23202747
## 149     TRUE  1.095927262 -0.57141953  0.21438371
## 150     TRUE  1.165555327 -1.22362837  0.18087555
## 151     TRUE  1.007069060  0.51736121  0.29235306
## 152     TRUE  1.045751319  0.15502297  0.27373742
## 153    FALSE  1.099906481 -0.35225058  0.24767551
## 154     TRUE  1.138588740 -0.71458882  0.22905987
## 155     TRUE  0.953135885  1.53544032  0.38872169
## 156    FALSE  1.076919113  0.37595793  0.32915163
## 157     TRUE  1.212418012 -0.63682315  0.29915024
## 158     TRUE  1.266573174 -1.14409669  0.27308834
## 159    FALSE  1.046195293  1.17663409  0.41435088
## 160     TRUE  1.030722390  1.32156939  0.42179713
## 161    FALSE  1.169978521  0.01715170  0.35478082
## 162    FALSE  1.010904502  1.90416401  0.48346936
## 163     TRUE  1.118627074  1.03563964  0.44855660
## 164     TRUE  1.180518688  0.45589844  0.41877157
## 165     TRUE  1.331490491 -0.70181793  0.38132392
## 166     TRUE  1.470746622 -2.00623561  0.31430760
## 167     TRUE  1.424327912 -1.57142972  0.33664637
## 168     TRUE  1.272990344  0.24311493  0.46161179
## 169     TRUE  1.496759692 -1.71242417  0.37085209
## 170    FALSE  1.365240013 -0.48047414  0.43414528
## 171     TRUE  1.195038075  1.11381414  0.51605412
## 172     TRUE  1.334294206 -0.19060354  0.44903780
## 173     TRUE  1.256929689  0.53407295  0.48626908
## 174     TRUE  1.512232596 -1.85735947  0.36340583
## 175    FALSE  1.380712916 -0.62540944  0.42669902
## 176    FALSE  1.256929689  0.53407295  0.48626908
## 177     TRUE  1.597768807 -1.70658663  0.46712031
## 178    FALSE  1.303783641  1.04718404  0.60859920
## 179     TRUE  1.473985580 -0.54710424  0.52669037
## 180     TRUE  1.312329833  1.33949923  0.65797176
## 181     TRUE  1.412315953  0.54344251  0.62678213
## 182     TRUE  1.427201104  0.54452993  0.63654691
## 183    FALSE  1.380782394  0.97933583  0.65888568
## 184    FALSE  1.605139494 -1.12222600  0.55091495
## 185    FALSE  1.589666591 -0.97729070  0.55836120
## 186    FALSE  1.574193687 -0.83235540  0.56580746
## 187     TRUE  1.419464653  0.61699758  0.64027004
## 188     TRUE  1.588704342 -0.24813384  0.66714544
## 189     TRUE  1.619650149 -0.53800444  0.65225292
## 190     TRUE  1.666068859 -0.97281033  0.62991415
## 191    FALSE  1.488130469  0.69394560  0.71554612
## 192     TRUE  1.580967890 -0.17566619  0.67086857
## 193    FALSE  1.598648024  0.26294148  0.75240266
## 194     TRUE  1.598648024  0.26294148  0.75240266
## 195     TRUE  1.621857380  0.04553853  0.74123327
## 196    FALSE  1.691485445 -0.60667031  0.70772511
## 197    FALSE  1.660539638 -0.31679971  0.72261763
# Means of scores for all the PC's classified by selfMade status
tabmeansPC <- aggregate(b_datatyp_pca[,2:4],by=list(selfMade=selfMade),mean)
tabmeansPC
##   selfMade         PC1         PC2          PC3
## 1    FALSE -0.11045638  0.12230507 -0.016949947
## 2     TRUE  0.05315194 -0.05885357  0.008156365
tabmeansPC <- tabmeansPC[rev(order(tabmeansPC$selfMade)),]
tabmeansPC
##   selfMade         PC1         PC2          PC3
## 2     TRUE  0.05315194 -0.05885357  0.008156365
## 1    FALSE -0.11045638  0.12230507 -0.016949947
excluded_columns_2 <- c(-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,-21,-22)
tabfmeans <- t(tabmeansPC[,excluded_columns_2])
tabfmeans
##                     2           1
## selfMade  1.000000000  0.00000000
## PC1       0.053151944 -0.11045638
## PC2      -0.058853566  0.12230507
## PC3       0.008156365 -0.01694995
colnames(tabfmeans) <- t(as.vector(tabmeansPC[1]$selfMade))
tabfmeans
##                  TRUE       FALSE
## selfMade  1.000000000  0.00000000
## PC1       0.053151944 -0.11045638
## PC2      -0.058853566  0.12230507
## PC3       0.008156365 -0.01694995
#Question 1
#Decide how many Principal Components (PCs) you want to keep and why (2 points)
#For this I am taking PC1 and PC2, because I need to calculate the rank based on age.




# Standard deviations of scores for all the PC's classified by selfmade status
tabsdsPC <- aggregate(b_datatyp_pca[,2:4],by=list(selfMade=selfMade),sd)
tabsdsPC
##   selfMade      PC1       PC2       PC3
## 1    FALSE 1.369311 0.9420996 0.6316469
## 2     TRUE 1.280954 1.0203907 0.4938381
tabfsds <- t(tabsdsPC[,excluded_columns])
colnames(tabfsds) <- t(as.vector(tabsdsPC[1]$selfMade))
tabfsds
##              FALSE     TRUE
## selfMade 0.0000000 1.000000
## PC1      1.3693110 1.280954
## PC2      0.9420996 1.020391
t.test(PC1~b_data$selfMade,data=b_datatyp_pca)
## 
##  Welch Two Sample t-test
## 
## data:  PC1 by b_data$selfMade
## t = -0.80183, df = 117.3, p-value = 0.4243
## alternative hypothesis: true difference in means between group FALSE and group TRUE is not equal to 0
## 95 percent confidence interval:
##  -0.5676973  0.2404806
## sample estimates:
## mean in group FALSE  mean in group TRUE 
##         -0.11045638          0.05315194
t.test(PC2~b_data$selfMade,data=b_datatyp_pca)
## 
##  Welch Two Sample t-test
## 
## data:  PC2 by b_data$selfMade
## t = 1.2299, df = 133.85, p-value = 0.2209
## alternative hypothesis: true difference in means between group FALSE and group TRUE is not equal to 0
## 95 percent confidence interval:
##  -0.1101731  0.4724904
## sample estimates:
## mean in group FALSE  mean in group TRUE 
##          0.12230507         -0.05885357
#t.test(PC3~b_data$selfMade,data=b_datatyp_pca)

## F ratio tests
var.test(PC1~b_data$selfMade,data=b_datatyp_pca)
## 
##  F test to compare two variances
## 
## data:  PC1 by b_data$selfMade
## F = 1.1427, num df = 63, denom df = 132, p-value = 0.5192
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.757440 1.781229
## sample estimates:
## ratio of variances 
##           1.142713
var.test(PC2~b_data$selfMade,data=b_datatyp_pca)
## 
##  F test to compare two variances
## 
## data:  PC2 by b_data$selfMade
## F = 0.85243, num df = 63, denom df = 132, p-value = 0.4824
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.5650306 1.3287504
## sample estimates:
## ratio of variances 
##          0.8524339
#var.test(PC3~b_data$selfMade,data=b_datatyp_pca)

# Levene's tests (one-sided)
library(car)
## Loading required package: carData
(LTPC1 <- leveneTest(PC1~b_data$selfMade,data=b_datatyp_pca))
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  0.3328 0.5647
##       195
(p_PC1_1sided <- LTPC1[[3]][1]/2)
## [1] 0.2823514
(LTPC2 <- leveneTest(PC2~b_data$selfMade,data=b_datatyp_pca))
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1  1.2915 0.2572
##       195
(p_PC2_1sided=LTPC2[[3]][1]/2)
## [1] 0.128584
#(LTPC3 <- leveneTest(PC3~b_data$selfMade,data=b_datatyp_pca))
#(p_PC3_1sided <- LTPC3[[3]][1]/2)
b_data
## # A tibble: 197 × 22
##     rank   age finalWorth birthYear birthMonth birthDay category      personName
##    <dbl> <dbl>      <dbl>     <dbl>      <dbl>    <dbl> <chr>         <chr>     
##  1     1    74     211000      1949          3        5 Fashion & Re… Bernard A…
##  2     2    51     180000      1971          6       28 Automotive    Elon Musk 
##  3     3    59     114000      1964          1       12 Technology    Jeff Bezos
##  4     4    78     107000      1944          8       17 Technology    Larry Ell…
##  5     5    92     106000      1930          8       30 Finance & In… Warren Bu…
##  6     6    67     104000      1955         10       28 Technology    Bill Gates
##  7     7    81      94500      1942          2       14 Media & Ente… Michael B…
##  8     8    83      93000      1940          1       28 Telecom       Carlos Sl…
##  9     9    65      83400      1957          4       19 Diversified   Mukesh Am…
## 10    10    67      80700      1956          3       24 Technology    Steve Bal…
## # ℹ 187 more rows
## # ℹ 14 more variables: country <chr>, city <chr>, source <chr>,
## #   industries <chr>, countryOfCitizenship <chr>, organization <chr>,
## #   selfMade <lgl>, gender <chr>, birthDate <chr>, state <chr>,
## #   residenceStateRegion <chr>, cpi_country <dbl>, cpi_change_country <dbl>,
## #   gdp_country <chr>
# Plotting the scores for the first and second components
plot(b_datatyp_pca$PC1, b_datatyp_pca$PC2,pch=ifelse(b_data_pca$selfMade == "True",1,16),xlab="PC1", ylab="PC2", main="133 Trues using PC1 & PC2")
abline(h=0)
abline(v=0)
legend("bottomleft", legend=c("True","False"), pch=c(1,16))

plot(eigen_b_data, xlab = "Component number", ylab = "Component variance", type = "l", main = "Scree diagram")

plot(log(eigen_b_data), xlab = "Component number",ylab = "log(Component variance)", type="l",main = "Log(eigenvalue) diagram")

print(summary(b_datatyp_pca))
##   selfMade            PC1               PC2                PC3          
##  Mode :logical   Min.   :-6.0188   Min.   :-2.84560   Min.   :-0.69822  
##  FALSE:64        1st Qu.:-0.6022   1st Qu.:-0.70182   1st Qu.:-0.43649  
##  TRUE :133       Median : 0.1842   Median : 0.04554   Median :-0.08201  
##                  Mean   : 0.0000   Mean   : 0.00000   Mean   : 0.00000  
##                  3rd Qu.: 1.0071   3rd Qu.: 0.68485   3rd Qu.: 0.32197  
##                  Max.   : 1.6915   Max.   : 2.32226   Max.   : 3.59192
# Assuming b_data_pca$x is a data frame or matrix
cov_matrix <- cov(b_data_pca$x)

# Extracting the diagonal elements
diagonal_cov <- diag(cov_matrix)

diag(cov(b_data_pca$x))
##       PC1       PC2       PC3 
## 1.7136414 0.9937337 0.2926249
xlim <- range(b_data_pca$x[,excluded_columns_2])
b_data_pca$x[,excluded_columns_2]
##                 PC1         PC2         PC3
##   [1,] -6.018762029 -0.40221757  3.59191907
##   [2,] -5.028184548 -1.94155766  2.71486049
##   [3,] -3.373832285 -1.09131018  1.04572055
##   [4,] -3.327800487  0.31487183  0.94731796
##   [5,] -3.397990109  1.33418580  0.98587088
##   [6,] -3.140640833 -0.46865024  0.85329324
##   [7,] -2.991383577  0.58541427  0.67145370
##   [8,] -2.955827138  0.73716061  0.65236480
##   [9,] -2.556421699 -0.52733082  0.34879229
##  [10,] -2.489884525 -0.37067853  0.29858916
##  [11,] -2.487890549 -0.22424698  0.31320735
##  [12,] -2.295032079 -1.59513894  0.22111845
##  [13,] -2.519924539  1.09461044  0.32196766
##  [14,] -2.180073486 -1.65316685  0.15913931
##  [15,] -2.108224408 -0.24289654  0.03480820
##  [16,] -1.770885226 -2.40152955 -0.15787072
##  [17,] -1.998254628  1.17214065 -0.10309380
##  [18,] -1.789370431 -0.78448587 -0.20361828
##  [19,] -1.867910453  0.23223606 -0.13196492
##  [20,] -1.855572101  0.52769120 -0.13582900
##  [21,] -1.781350867  0.16971101 -0.16542268
##  [22,] -1.647776087 -0.39994851 -0.24248568
##  [23,] -1.470303199 -0.88896378 -0.36768261
##  [24,] -1.398602569 -0.73149382 -0.42307143
##  [25,] -1.525494151  1.08946142 -0.37208548
##  [26,] -1.154732220 -2.24296302 -0.53358463
##  [27,] -1.428616386  0.95487756 -0.41185923
##  [28,] -1.367233935  1.18440633 -0.46093207
##  [29,] -1.321376780  1.11670557 -0.47822609
##  [30,] -1.095025704 -0.83842471 -0.57157864
##  [31,] -1.260643206  0.97639809 -0.48169990
##  [32,] -1.291589013  1.26626868 -0.46680739
##  [33,] -1.313392145  1.77612589 -0.42380878
##  [34,] -0.898714642 -1.32826602 -0.64155270
##  [35,] -1.079813780  0.55779794 -0.54017227
##  [36,] -0.901875390 -1.10895799 -0.62580424
##  [37,] -0.683848518 -2.84559790 -0.69822262
##  [38,] -0.823657472 -1.10038095 -0.63021697
##  [39,] -0.945429260  0.35292126 -0.56413958
##  [40,] -0.850589131 -0.29656484 -0.59825437
##  [41,] -0.895005134  0.35836674 -0.56535284
##  [42,] -0.879532230  0.21343144 -0.57279910
##  [43,] -0.921962989  0.94110043 -0.52122395
##  [44,] -0.891017182  0.65122983 -0.53611647
##  [45,] -0.569817245 -1.87969241 -0.66763924
##  [46,] -0.885009058  1.31160689 -0.50442819
##  [47,] -0.929433793  1.89284433 -0.46747124
##  [48,] -0.651483086 -0.34888590 -0.59645912
##  [49,] -0.783581785  1.10278099 -0.52001032
##  [50,] -0.701641564  0.52494488 -0.54521627
##  [51,] -0.555228001 -0.63222360 -0.60280011
##  [52,] -0.602225732  0.02229915 -0.56730573
##  [53,] -0.442920993 -1.28021345 -0.62974297
##  [54,] -0.567291973  0.02529165 -0.55296187
##  [55,] -0.591062884  0.60979973 -0.53674773
##  [56,] -0.552968378  0.39348420 -0.53815234
##  [57,] -0.566447305  0.68485105 -0.51608789
##  [58,] -0.342677957 -1.27068806 -0.60684759
##  [59,] -0.353566424 -0.90521825 -0.59143143
##  [60,] -0.305153738 -1.19359259 -0.59915201
##  [61,] -0.519780411  0.98193313 -0.48028622
##  [62,] -0.339839313 -0.46469711 -0.55535543
##  [63,] -0.291426627 -0.75307146 -0.56307601
##  [64,] -0.446743414  0.84230424 -0.47140240
##  [65,] -0.426694807  0.84420932 -0.46682332
##  [66,] -0.449904162  1.06161226 -0.45565394
##  [67,] -0.319559986  0.12170766 -0.48452505
##  [68,] -0.384612348  0.92075688 -0.43899155
##  [69,] -0.243362241 -0.23722925 -0.49138969
##  [70,] -0.231050086 -0.16285653 -0.48308748
##  [71,] -0.107854610 -1.17631619 -0.52544651
##  [72,] -0.154852340 -0.52179344 -0.48995212
##  [73,] -0.201271051 -0.08698755 -0.46761335
##  [74,]  0.073492712 -2.33049217 -0.56868649
##  [75,] -0.050290516 -1.17100979 -0.50911642
##  [76,] -0.084993555 -0.58950258 -0.45720898
##  [77,] -0.302201956  1.58561431 -0.33575033
##  [78,] -0.201628084  0.64353487 -0.38415101
##  [79,] -0.019710474 -0.80405210 -0.43649117
##  [80,] -0.113126915  0.28527654 -0.37865801
##  [81,] -0.185915729  1.15679341 -0.32940139
##  [82,]  0.019816452 -0.50683095 -0.41823284
##  [83,] -0.047808905  0.36550358 -0.37416192
##  [84,]  0.075386570 -0.64795609 -0.41652094
##  [85,]  0.106332377 -0.93782669 -0.43141346
##  [86,]  0.002001273  0.29588935 -0.34599785
##  [87,]  0.118048049 -0.79112538 -0.40184479
##  [88,]  0.147818351 -0.78895054 -0.38231522
##  [89,]  0.010556196  0.66189869 -0.30068072
##  [90,]  0.103393617 -0.20771310 -0.34535826
##  [91,]  0.064711358  0.15462514 -0.32674262
##  [92,]  0.184158334 -0.49350378 -0.33614214
##  [93,]  0.119105972  0.30554544 -0.29060865
##  [94,]  0.182991561 -0.12776421 -0.30577549
##  [95,]  0.020526075  1.39405642 -0.22758979
##  [96,]  0.128836399  0.37950934 -0.27971359
##  [97,]  0.036808718  1.61390397 -0.18194035
##  [98,]  0.090963880  1.10663043 -0.20800225
##  [99,]  0.053687845  1.76142293 -0.15755738
## [100,]  0.246520118  0.16944856 -0.23748000
## [101,]  0.248514094  0.31588011 -0.22286181
## [102,]  0.380033774 -0.91606992 -0.28615500
## [103,]  0.278284397  0.31805495 -0.20333225
## [104,]  0.269960193  0.53654532 -0.18239808
## [105,]  0.331851807 -0.04319588 -0.21218311
## [106,]  0.454459531 -0.91063283 -0.23733109
## [107,]  0.376507261 -0.03993362 -0.18288877
## [108,]  0.267609185  1.12063619 -0.11355393
## [109,]  0.321764347  0.61336264 -0.13961583
## [110,]  0.445769562 -0.03531418 -0.12887916
## [111,]  0.630856651 -1.62851504 -0.20102321
## [112,]  0.470385141  0.03973714 -0.10821932
## [113,]  0.578107713 -0.82878723 -0.14313208
## [114,]  0.389272123  1.12974437 -0.03802853
## [115,]  0.404745027  0.98480907 -0.04547479
## [116,]  0.455142957  0.76917213 -0.03452176
## [117,]  0.547392625  0.04558306 -0.06198827
## [118,]  0.624169391 -0.53307071 -0.08200852
## [119,]  0.646790994 -0.60445094 -0.07596686
## [120,]  0.584311628  0.12131297 -0.02897080
## [121,]  0.398049034  2.00655926  0.07759534
## [122,]  0.722392255 -0.89105928 -0.06156504
## [123,]  0.645027737 -0.16638279 -0.02433375
## [124,]  0.455595663  1.86447739  0.10203627
## [125,]  0.671628560 -0.01859406  0.01499970
## [126,]  0.763878228 -0.74218313 -0.01246680
## [127,]  0.477629514  1.93911988  0.12528896
## [128,]  0.631183045  0.78181233  0.08524846
## [129,]  0.646655948  0.63687704  0.07780221
## [130,]  0.871013049 -1.46468479 -0.03016853
## [131,]  0.662947323  0.93041872  0.11939621
## [132,]  0.662947323  0.93041872  0.11939621
## [133,]  0.732575389  0.27820988  0.08588805
## [134,]  0.857168356 -0.51648966  0.07941368
## [135,]  0.639959955  1.65862723  0.20087233
## [136,]  0.858574580 -0.22403540  0.11124291
## [137,]  0.773473611  0.57310874  0.15219733
## [138,]  0.937345321 -0.65625762  0.10584085
## [139,]  0.965130380 -0.72682019  0.10669679
## [140,]  1.034170693 -1.23300632  0.09039967
## [141,]  0.887178110  0.14387901  0.16113912
## [142,]  0.891157330  0.36304797  0.19443092
## [143,]  1.006616354 -0.57794405  0.15579503
## [144,]  0.714625163  2.32225816  0.31189211
## [145,]  0.863611722  1.09180438  0.25577085
## [146,]  1.218900750 -2.09568476  0.10171796
## [147,]  0.948124939  0.44068296  0.23202747
## [148,]  0.948124939  0.44068296  0.23202747
## [149,]  1.095927262 -0.57141953  0.21438371
## [150,]  1.165555327 -1.22362837  0.18087555
## [151,]  1.007069060  0.51736121  0.29235306
## [152,]  1.045751319  0.15502297  0.27373742
## [153,]  1.099906481 -0.35225058  0.24767551
## [154,]  1.138588740 -0.71458882  0.22905987
## [155,]  0.953135885  1.53544032  0.38872169
## [156,]  1.076919113  0.37595793  0.32915163
## [157,]  1.212418012 -0.63682315  0.29915024
## [158,]  1.266573174 -1.14409669  0.27308834
## [159,]  1.046195293  1.17663409  0.41435088
## [160,]  1.030722390  1.32156939  0.42179713
## [161,]  1.169978521  0.01715170  0.35478082
## [162,]  1.010904502  1.90416401  0.48346936
## [163,]  1.118627074  1.03563964  0.44855660
## [164,]  1.180518688  0.45589844  0.41877157
## [165,]  1.331490491 -0.70181793  0.38132392
## [166,]  1.470746622 -2.00623561  0.31430760
## [167,]  1.424327912 -1.57142972  0.33664637
## [168,]  1.272990344  0.24311493  0.46161179
## [169,]  1.496759692 -1.71242417  0.37085209
## [170,]  1.365240013 -0.48047414  0.43414528
## [171,]  1.195038075  1.11381414  0.51605412
## [172,]  1.334294206 -0.19060354  0.44903780
## [173,]  1.256929689  0.53407295  0.48626908
## [174,]  1.512232596 -1.85735947  0.36340583
## [175,]  1.380712916 -0.62540944  0.42669902
## [176,]  1.256929689  0.53407295  0.48626908
## [177,]  1.597768807 -1.70658663  0.46712031
## [178,]  1.303783641  1.04718404  0.60859920
## [179,]  1.473985580 -0.54710424  0.52669037
## [180,]  1.312329833  1.33949923  0.65797176
## [181,]  1.412315953  0.54344251  0.62678213
## [182,]  1.427201104  0.54452993  0.63654691
## [183,]  1.380782394  0.97933583  0.65888568
## [184,]  1.605139494 -1.12222600  0.55091495
## [185,]  1.589666591 -0.97729070  0.55836120
## [186,]  1.574193687 -0.83235540  0.56580746
## [187,]  1.419464653  0.61699758  0.64027004
## [188,]  1.588704342 -0.24813384  0.66714544
## [189,]  1.619650149 -0.53800444  0.65225292
## [190,]  1.666068859 -0.97281033  0.62991415
## [191,]  1.488130469  0.69394560  0.71554612
## [192,]  1.580967890 -0.17566619  0.67086857
## [193,]  1.598648024  0.26294148  0.75240266
## [194,]  1.598648024  0.26294148  0.75240266
## [195,]  1.621857380  0.04553853  0.74123327
## [196,]  1.691485445 -0.60667031  0.70772511
## [197,]  1.660539638 -0.31679971  0.72261763
b_data_pca$x
##                 PC1         PC2         PC3
##   [1,] -6.018762029 -0.40221757  3.59191907
##   [2,] -5.028184548 -1.94155766  2.71486049
##   [3,] -3.373832285 -1.09131018  1.04572055
##   [4,] -3.327800487  0.31487183  0.94731796
##   [5,] -3.397990109  1.33418580  0.98587088
##   [6,] -3.140640833 -0.46865024  0.85329324
##   [7,] -2.991383577  0.58541427  0.67145370
##   [8,] -2.955827138  0.73716061  0.65236480
##   [9,] -2.556421699 -0.52733082  0.34879229
##  [10,] -2.489884525 -0.37067853  0.29858916
##  [11,] -2.487890549 -0.22424698  0.31320735
##  [12,] -2.295032079 -1.59513894  0.22111845
##  [13,] -2.519924539  1.09461044  0.32196766
##  [14,] -2.180073486 -1.65316685  0.15913931
##  [15,] -2.108224408 -0.24289654  0.03480820
##  [16,] -1.770885226 -2.40152955 -0.15787072
##  [17,] -1.998254628  1.17214065 -0.10309380
##  [18,] -1.789370431 -0.78448587 -0.20361828
##  [19,] -1.867910453  0.23223606 -0.13196492
##  [20,] -1.855572101  0.52769120 -0.13582900
##  [21,] -1.781350867  0.16971101 -0.16542268
##  [22,] -1.647776087 -0.39994851 -0.24248568
##  [23,] -1.470303199 -0.88896378 -0.36768261
##  [24,] -1.398602569 -0.73149382 -0.42307143
##  [25,] -1.525494151  1.08946142 -0.37208548
##  [26,] -1.154732220 -2.24296302 -0.53358463
##  [27,] -1.428616386  0.95487756 -0.41185923
##  [28,] -1.367233935  1.18440633 -0.46093207
##  [29,] -1.321376780  1.11670557 -0.47822609
##  [30,] -1.095025704 -0.83842471 -0.57157864
##  [31,] -1.260643206  0.97639809 -0.48169990
##  [32,] -1.291589013  1.26626868 -0.46680739
##  [33,] -1.313392145  1.77612589 -0.42380878
##  [34,] -0.898714642 -1.32826602 -0.64155270
##  [35,] -1.079813780  0.55779794 -0.54017227
##  [36,] -0.901875390 -1.10895799 -0.62580424
##  [37,] -0.683848518 -2.84559790 -0.69822262
##  [38,] -0.823657472 -1.10038095 -0.63021697
##  [39,] -0.945429260  0.35292126 -0.56413958
##  [40,] -0.850589131 -0.29656484 -0.59825437
##  [41,] -0.895005134  0.35836674 -0.56535284
##  [42,] -0.879532230  0.21343144 -0.57279910
##  [43,] -0.921962989  0.94110043 -0.52122395
##  [44,] -0.891017182  0.65122983 -0.53611647
##  [45,] -0.569817245 -1.87969241 -0.66763924
##  [46,] -0.885009058  1.31160689 -0.50442819
##  [47,] -0.929433793  1.89284433 -0.46747124
##  [48,] -0.651483086 -0.34888590 -0.59645912
##  [49,] -0.783581785  1.10278099 -0.52001032
##  [50,] -0.701641564  0.52494488 -0.54521627
##  [51,] -0.555228001 -0.63222360 -0.60280011
##  [52,] -0.602225732  0.02229915 -0.56730573
##  [53,] -0.442920993 -1.28021345 -0.62974297
##  [54,] -0.567291973  0.02529165 -0.55296187
##  [55,] -0.591062884  0.60979973 -0.53674773
##  [56,] -0.552968378  0.39348420 -0.53815234
##  [57,] -0.566447305  0.68485105 -0.51608789
##  [58,] -0.342677957 -1.27068806 -0.60684759
##  [59,] -0.353566424 -0.90521825 -0.59143143
##  [60,] -0.305153738 -1.19359259 -0.59915201
##  [61,] -0.519780411  0.98193313 -0.48028622
##  [62,] -0.339839313 -0.46469711 -0.55535543
##  [63,] -0.291426627 -0.75307146 -0.56307601
##  [64,] -0.446743414  0.84230424 -0.47140240
##  [65,] -0.426694807  0.84420932 -0.46682332
##  [66,] -0.449904162  1.06161226 -0.45565394
##  [67,] -0.319559986  0.12170766 -0.48452505
##  [68,] -0.384612348  0.92075688 -0.43899155
##  [69,] -0.243362241 -0.23722925 -0.49138969
##  [70,] -0.231050086 -0.16285653 -0.48308748
##  [71,] -0.107854610 -1.17631619 -0.52544651
##  [72,] -0.154852340 -0.52179344 -0.48995212
##  [73,] -0.201271051 -0.08698755 -0.46761335
##  [74,]  0.073492712 -2.33049217 -0.56868649
##  [75,] -0.050290516 -1.17100979 -0.50911642
##  [76,] -0.084993555 -0.58950258 -0.45720898
##  [77,] -0.302201956  1.58561431 -0.33575033
##  [78,] -0.201628084  0.64353487 -0.38415101
##  [79,] -0.019710474 -0.80405210 -0.43649117
##  [80,] -0.113126915  0.28527654 -0.37865801
##  [81,] -0.185915729  1.15679341 -0.32940139
##  [82,]  0.019816452 -0.50683095 -0.41823284
##  [83,] -0.047808905  0.36550358 -0.37416192
##  [84,]  0.075386570 -0.64795609 -0.41652094
##  [85,]  0.106332377 -0.93782669 -0.43141346
##  [86,]  0.002001273  0.29588935 -0.34599785
##  [87,]  0.118048049 -0.79112538 -0.40184479
##  [88,]  0.147818351 -0.78895054 -0.38231522
##  [89,]  0.010556196  0.66189869 -0.30068072
##  [90,]  0.103393617 -0.20771310 -0.34535826
##  [91,]  0.064711358  0.15462514 -0.32674262
##  [92,]  0.184158334 -0.49350378 -0.33614214
##  [93,]  0.119105972  0.30554544 -0.29060865
##  [94,]  0.182991561 -0.12776421 -0.30577549
##  [95,]  0.020526075  1.39405642 -0.22758979
##  [96,]  0.128836399  0.37950934 -0.27971359
##  [97,]  0.036808718  1.61390397 -0.18194035
##  [98,]  0.090963880  1.10663043 -0.20800225
##  [99,]  0.053687845  1.76142293 -0.15755738
## [100,]  0.246520118  0.16944856 -0.23748000
## [101,]  0.248514094  0.31588011 -0.22286181
## [102,]  0.380033774 -0.91606992 -0.28615500
## [103,]  0.278284397  0.31805495 -0.20333225
## [104,]  0.269960193  0.53654532 -0.18239808
## [105,]  0.331851807 -0.04319588 -0.21218311
## [106,]  0.454459531 -0.91063283 -0.23733109
## [107,]  0.376507261 -0.03993362 -0.18288877
## [108,]  0.267609185  1.12063619 -0.11355393
## [109,]  0.321764347  0.61336264 -0.13961583
## [110,]  0.445769562 -0.03531418 -0.12887916
## [111,]  0.630856651 -1.62851504 -0.20102321
## [112,]  0.470385141  0.03973714 -0.10821932
## [113,]  0.578107713 -0.82878723 -0.14313208
## [114,]  0.389272123  1.12974437 -0.03802853
## [115,]  0.404745027  0.98480907 -0.04547479
## [116,]  0.455142957  0.76917213 -0.03452176
## [117,]  0.547392625  0.04558306 -0.06198827
## [118,]  0.624169391 -0.53307071 -0.08200852
## [119,]  0.646790994 -0.60445094 -0.07596686
## [120,]  0.584311628  0.12131297 -0.02897080
## [121,]  0.398049034  2.00655926  0.07759534
## [122,]  0.722392255 -0.89105928 -0.06156504
## [123,]  0.645027737 -0.16638279 -0.02433375
## [124,]  0.455595663  1.86447739  0.10203627
## [125,]  0.671628560 -0.01859406  0.01499970
## [126,]  0.763878228 -0.74218313 -0.01246680
## [127,]  0.477629514  1.93911988  0.12528896
## [128,]  0.631183045  0.78181233  0.08524846
## [129,]  0.646655948  0.63687704  0.07780221
## [130,]  0.871013049 -1.46468479 -0.03016853
## [131,]  0.662947323  0.93041872  0.11939621
## [132,]  0.662947323  0.93041872  0.11939621
## [133,]  0.732575389  0.27820988  0.08588805
## [134,]  0.857168356 -0.51648966  0.07941368
## [135,]  0.639959955  1.65862723  0.20087233
## [136,]  0.858574580 -0.22403540  0.11124291
## [137,]  0.773473611  0.57310874  0.15219733
## [138,]  0.937345321 -0.65625762  0.10584085
## [139,]  0.965130380 -0.72682019  0.10669679
## [140,]  1.034170693 -1.23300632  0.09039967
## [141,]  0.887178110  0.14387901  0.16113912
## [142,]  0.891157330  0.36304797  0.19443092
## [143,]  1.006616354 -0.57794405  0.15579503
## [144,]  0.714625163  2.32225816  0.31189211
## [145,]  0.863611722  1.09180438  0.25577085
## [146,]  1.218900750 -2.09568476  0.10171796
## [147,]  0.948124939  0.44068296  0.23202747
## [148,]  0.948124939  0.44068296  0.23202747
## [149,]  1.095927262 -0.57141953  0.21438371
## [150,]  1.165555327 -1.22362837  0.18087555
## [151,]  1.007069060  0.51736121  0.29235306
## [152,]  1.045751319  0.15502297  0.27373742
## [153,]  1.099906481 -0.35225058  0.24767551
## [154,]  1.138588740 -0.71458882  0.22905987
## [155,]  0.953135885  1.53544032  0.38872169
## [156,]  1.076919113  0.37595793  0.32915163
## [157,]  1.212418012 -0.63682315  0.29915024
## [158,]  1.266573174 -1.14409669  0.27308834
## [159,]  1.046195293  1.17663409  0.41435088
## [160,]  1.030722390  1.32156939  0.42179713
## [161,]  1.169978521  0.01715170  0.35478082
## [162,]  1.010904502  1.90416401  0.48346936
## [163,]  1.118627074  1.03563964  0.44855660
## [164,]  1.180518688  0.45589844  0.41877157
## [165,]  1.331490491 -0.70181793  0.38132392
## [166,]  1.470746622 -2.00623561  0.31430760
## [167,]  1.424327912 -1.57142972  0.33664637
## [168,]  1.272990344  0.24311493  0.46161179
## [169,]  1.496759692 -1.71242417  0.37085209
## [170,]  1.365240013 -0.48047414  0.43414528
## [171,]  1.195038075  1.11381414  0.51605412
## [172,]  1.334294206 -0.19060354  0.44903780
## [173,]  1.256929689  0.53407295  0.48626908
## [174,]  1.512232596 -1.85735947  0.36340583
## [175,]  1.380712916 -0.62540944  0.42669902
## [176,]  1.256929689  0.53407295  0.48626908
## [177,]  1.597768807 -1.70658663  0.46712031
## [178,]  1.303783641  1.04718404  0.60859920
## [179,]  1.473985580 -0.54710424  0.52669037
## [180,]  1.312329833  1.33949923  0.65797176
## [181,]  1.412315953  0.54344251  0.62678213
## [182,]  1.427201104  0.54452993  0.63654691
## [183,]  1.380782394  0.97933583  0.65888568
## [184,]  1.605139494 -1.12222600  0.55091495
## [185,]  1.589666591 -0.97729070  0.55836120
## [186,]  1.574193687 -0.83235540  0.56580746
## [187,]  1.419464653  0.61699758  0.64027004
## [188,]  1.588704342 -0.24813384  0.66714544
## [189,]  1.619650149 -0.53800444  0.65225292
## [190,]  1.666068859 -0.97281033  0.62991415
## [191,]  1.488130469  0.69394560  0.71554612
## [192,]  1.580967890 -0.17566619  0.67086857
## [193,]  1.598648024  0.26294148  0.75240266
## [194,]  1.598648024  0.26294148  0.75240266
## [195,]  1.621857380  0.04553853  0.74123327
## [196,]  1.691485445 -0.60667031  0.70772511
## [197,]  1.660539638 -0.31679971  0.72261763
plot(b_data_pca$x,xlim=xlim,ylim=xlim)

b_data_pca$rotation[,1]
##       rank        age finalWorth 
##  0.7050171 -0.1060159 -0.7012215
b_data_pca$rotation
##                   PC1         PC2       PC3
## rank        0.7050171  0.03888488 0.7081235
## age        -0.1060159  0.99305470 0.0510196
## finalWorth -0.7012215 -0.11104201 0.7042430
plot(b_data[,excluded_columns_2])

b_data_pca$x
##                 PC1         PC2         PC3
##   [1,] -6.018762029 -0.40221757  3.59191907
##   [2,] -5.028184548 -1.94155766  2.71486049
##   [3,] -3.373832285 -1.09131018  1.04572055
##   [4,] -3.327800487  0.31487183  0.94731796
##   [5,] -3.397990109  1.33418580  0.98587088
##   [6,] -3.140640833 -0.46865024  0.85329324
##   [7,] -2.991383577  0.58541427  0.67145370
##   [8,] -2.955827138  0.73716061  0.65236480
##   [9,] -2.556421699 -0.52733082  0.34879229
##  [10,] -2.489884525 -0.37067853  0.29858916
##  [11,] -2.487890549 -0.22424698  0.31320735
##  [12,] -2.295032079 -1.59513894  0.22111845
##  [13,] -2.519924539  1.09461044  0.32196766
##  [14,] -2.180073486 -1.65316685  0.15913931
##  [15,] -2.108224408 -0.24289654  0.03480820
##  [16,] -1.770885226 -2.40152955 -0.15787072
##  [17,] -1.998254628  1.17214065 -0.10309380
##  [18,] -1.789370431 -0.78448587 -0.20361828
##  [19,] -1.867910453  0.23223606 -0.13196492
##  [20,] -1.855572101  0.52769120 -0.13582900
##  [21,] -1.781350867  0.16971101 -0.16542268
##  [22,] -1.647776087 -0.39994851 -0.24248568
##  [23,] -1.470303199 -0.88896378 -0.36768261
##  [24,] -1.398602569 -0.73149382 -0.42307143
##  [25,] -1.525494151  1.08946142 -0.37208548
##  [26,] -1.154732220 -2.24296302 -0.53358463
##  [27,] -1.428616386  0.95487756 -0.41185923
##  [28,] -1.367233935  1.18440633 -0.46093207
##  [29,] -1.321376780  1.11670557 -0.47822609
##  [30,] -1.095025704 -0.83842471 -0.57157864
##  [31,] -1.260643206  0.97639809 -0.48169990
##  [32,] -1.291589013  1.26626868 -0.46680739
##  [33,] -1.313392145  1.77612589 -0.42380878
##  [34,] -0.898714642 -1.32826602 -0.64155270
##  [35,] -1.079813780  0.55779794 -0.54017227
##  [36,] -0.901875390 -1.10895799 -0.62580424
##  [37,] -0.683848518 -2.84559790 -0.69822262
##  [38,] -0.823657472 -1.10038095 -0.63021697
##  [39,] -0.945429260  0.35292126 -0.56413958
##  [40,] -0.850589131 -0.29656484 -0.59825437
##  [41,] -0.895005134  0.35836674 -0.56535284
##  [42,] -0.879532230  0.21343144 -0.57279910
##  [43,] -0.921962989  0.94110043 -0.52122395
##  [44,] -0.891017182  0.65122983 -0.53611647
##  [45,] -0.569817245 -1.87969241 -0.66763924
##  [46,] -0.885009058  1.31160689 -0.50442819
##  [47,] -0.929433793  1.89284433 -0.46747124
##  [48,] -0.651483086 -0.34888590 -0.59645912
##  [49,] -0.783581785  1.10278099 -0.52001032
##  [50,] -0.701641564  0.52494488 -0.54521627
##  [51,] -0.555228001 -0.63222360 -0.60280011
##  [52,] -0.602225732  0.02229915 -0.56730573
##  [53,] -0.442920993 -1.28021345 -0.62974297
##  [54,] -0.567291973  0.02529165 -0.55296187
##  [55,] -0.591062884  0.60979973 -0.53674773
##  [56,] -0.552968378  0.39348420 -0.53815234
##  [57,] -0.566447305  0.68485105 -0.51608789
##  [58,] -0.342677957 -1.27068806 -0.60684759
##  [59,] -0.353566424 -0.90521825 -0.59143143
##  [60,] -0.305153738 -1.19359259 -0.59915201
##  [61,] -0.519780411  0.98193313 -0.48028622
##  [62,] -0.339839313 -0.46469711 -0.55535543
##  [63,] -0.291426627 -0.75307146 -0.56307601
##  [64,] -0.446743414  0.84230424 -0.47140240
##  [65,] -0.426694807  0.84420932 -0.46682332
##  [66,] -0.449904162  1.06161226 -0.45565394
##  [67,] -0.319559986  0.12170766 -0.48452505
##  [68,] -0.384612348  0.92075688 -0.43899155
##  [69,] -0.243362241 -0.23722925 -0.49138969
##  [70,] -0.231050086 -0.16285653 -0.48308748
##  [71,] -0.107854610 -1.17631619 -0.52544651
##  [72,] -0.154852340 -0.52179344 -0.48995212
##  [73,] -0.201271051 -0.08698755 -0.46761335
##  [74,]  0.073492712 -2.33049217 -0.56868649
##  [75,] -0.050290516 -1.17100979 -0.50911642
##  [76,] -0.084993555 -0.58950258 -0.45720898
##  [77,] -0.302201956  1.58561431 -0.33575033
##  [78,] -0.201628084  0.64353487 -0.38415101
##  [79,] -0.019710474 -0.80405210 -0.43649117
##  [80,] -0.113126915  0.28527654 -0.37865801
##  [81,] -0.185915729  1.15679341 -0.32940139
##  [82,]  0.019816452 -0.50683095 -0.41823284
##  [83,] -0.047808905  0.36550358 -0.37416192
##  [84,]  0.075386570 -0.64795609 -0.41652094
##  [85,]  0.106332377 -0.93782669 -0.43141346
##  [86,]  0.002001273  0.29588935 -0.34599785
##  [87,]  0.118048049 -0.79112538 -0.40184479
##  [88,]  0.147818351 -0.78895054 -0.38231522
##  [89,]  0.010556196  0.66189869 -0.30068072
##  [90,]  0.103393617 -0.20771310 -0.34535826
##  [91,]  0.064711358  0.15462514 -0.32674262
##  [92,]  0.184158334 -0.49350378 -0.33614214
##  [93,]  0.119105972  0.30554544 -0.29060865
##  [94,]  0.182991561 -0.12776421 -0.30577549
##  [95,]  0.020526075  1.39405642 -0.22758979
##  [96,]  0.128836399  0.37950934 -0.27971359
##  [97,]  0.036808718  1.61390397 -0.18194035
##  [98,]  0.090963880  1.10663043 -0.20800225
##  [99,]  0.053687845  1.76142293 -0.15755738
## [100,]  0.246520118  0.16944856 -0.23748000
## [101,]  0.248514094  0.31588011 -0.22286181
## [102,]  0.380033774 -0.91606992 -0.28615500
## [103,]  0.278284397  0.31805495 -0.20333225
## [104,]  0.269960193  0.53654532 -0.18239808
## [105,]  0.331851807 -0.04319588 -0.21218311
## [106,]  0.454459531 -0.91063283 -0.23733109
## [107,]  0.376507261 -0.03993362 -0.18288877
## [108,]  0.267609185  1.12063619 -0.11355393
## [109,]  0.321764347  0.61336264 -0.13961583
## [110,]  0.445769562 -0.03531418 -0.12887916
## [111,]  0.630856651 -1.62851504 -0.20102321
## [112,]  0.470385141  0.03973714 -0.10821932
## [113,]  0.578107713 -0.82878723 -0.14313208
## [114,]  0.389272123  1.12974437 -0.03802853
## [115,]  0.404745027  0.98480907 -0.04547479
## [116,]  0.455142957  0.76917213 -0.03452176
## [117,]  0.547392625  0.04558306 -0.06198827
## [118,]  0.624169391 -0.53307071 -0.08200852
## [119,]  0.646790994 -0.60445094 -0.07596686
## [120,]  0.584311628  0.12131297 -0.02897080
## [121,]  0.398049034  2.00655926  0.07759534
## [122,]  0.722392255 -0.89105928 -0.06156504
## [123,]  0.645027737 -0.16638279 -0.02433375
## [124,]  0.455595663  1.86447739  0.10203627
## [125,]  0.671628560 -0.01859406  0.01499970
## [126,]  0.763878228 -0.74218313 -0.01246680
## [127,]  0.477629514  1.93911988  0.12528896
## [128,]  0.631183045  0.78181233  0.08524846
## [129,]  0.646655948  0.63687704  0.07780221
## [130,]  0.871013049 -1.46468479 -0.03016853
## [131,]  0.662947323  0.93041872  0.11939621
## [132,]  0.662947323  0.93041872  0.11939621
## [133,]  0.732575389  0.27820988  0.08588805
## [134,]  0.857168356 -0.51648966  0.07941368
## [135,]  0.639959955  1.65862723  0.20087233
## [136,]  0.858574580 -0.22403540  0.11124291
## [137,]  0.773473611  0.57310874  0.15219733
## [138,]  0.937345321 -0.65625762  0.10584085
## [139,]  0.965130380 -0.72682019  0.10669679
## [140,]  1.034170693 -1.23300632  0.09039967
## [141,]  0.887178110  0.14387901  0.16113912
## [142,]  0.891157330  0.36304797  0.19443092
## [143,]  1.006616354 -0.57794405  0.15579503
## [144,]  0.714625163  2.32225816  0.31189211
## [145,]  0.863611722  1.09180438  0.25577085
## [146,]  1.218900750 -2.09568476  0.10171796
## [147,]  0.948124939  0.44068296  0.23202747
## [148,]  0.948124939  0.44068296  0.23202747
## [149,]  1.095927262 -0.57141953  0.21438371
## [150,]  1.165555327 -1.22362837  0.18087555
## [151,]  1.007069060  0.51736121  0.29235306
## [152,]  1.045751319  0.15502297  0.27373742
## [153,]  1.099906481 -0.35225058  0.24767551
## [154,]  1.138588740 -0.71458882  0.22905987
## [155,]  0.953135885  1.53544032  0.38872169
## [156,]  1.076919113  0.37595793  0.32915163
## [157,]  1.212418012 -0.63682315  0.29915024
## [158,]  1.266573174 -1.14409669  0.27308834
## [159,]  1.046195293  1.17663409  0.41435088
## [160,]  1.030722390  1.32156939  0.42179713
## [161,]  1.169978521  0.01715170  0.35478082
## [162,]  1.010904502  1.90416401  0.48346936
## [163,]  1.118627074  1.03563964  0.44855660
## [164,]  1.180518688  0.45589844  0.41877157
## [165,]  1.331490491 -0.70181793  0.38132392
## [166,]  1.470746622 -2.00623561  0.31430760
## [167,]  1.424327912 -1.57142972  0.33664637
## [168,]  1.272990344  0.24311493  0.46161179
## [169,]  1.496759692 -1.71242417  0.37085209
## [170,]  1.365240013 -0.48047414  0.43414528
## [171,]  1.195038075  1.11381414  0.51605412
## [172,]  1.334294206 -0.19060354  0.44903780
## [173,]  1.256929689  0.53407295  0.48626908
## [174,]  1.512232596 -1.85735947  0.36340583
## [175,]  1.380712916 -0.62540944  0.42669902
## [176,]  1.256929689  0.53407295  0.48626908
## [177,]  1.597768807 -1.70658663  0.46712031
## [178,]  1.303783641  1.04718404  0.60859920
## [179,]  1.473985580 -0.54710424  0.52669037
## [180,]  1.312329833  1.33949923  0.65797176
## [181,]  1.412315953  0.54344251  0.62678213
## [182,]  1.427201104  0.54452993  0.63654691
## [183,]  1.380782394  0.97933583  0.65888568
## [184,]  1.605139494 -1.12222600  0.55091495
## [185,]  1.589666591 -0.97729070  0.55836120
## [186,]  1.574193687 -0.83235540  0.56580746
## [187,]  1.419464653  0.61699758  0.64027004
## [188,]  1.588704342 -0.24813384  0.66714544
## [189,]  1.619650149 -0.53800444  0.65225292
## [190,]  1.666068859 -0.97281033  0.62991415
## [191,]  1.488130469  0.69394560  0.71554612
## [192,]  1.580967890 -0.17566619  0.67086857
## [193,]  1.598648024  0.26294148  0.75240266
## [194,]  1.598648024  0.26294148  0.75240266
## [195,]  1.621857380  0.04553853  0.74123327
## [196,]  1.691485445 -0.60667031  0.70772511
## [197,]  1.660539638 -0.31679971  0.72261763
plot(b_data_pca)

#Explain the variate representation each PCs (4 points)

#get the original value of the data based on PCA
center <- b_data_pca$center
center
##        rank         age  finalWorth 
##    99.29949    68.21827 26448.73096
scale <- b_data_pca$scale
scale
##        rank         age  finalWorth 
##    57.30251    13.70342 27160.93497
new_b_data <- as.matrix(b_data[,excluded_columns])
new_b_data
##        rank age finalWorth
##   [1,]    1  74     211000
##   [2,]    2  51     180000
##   [3,]    3  59     114000
##   [4,]    4  78     107000
##   [5,]    5  92     106000
##   [6,]    6  67     104000
##   [7,]    7  81      94500
##   [8,]    8  83      93000
##   [9,]    9  65      83400
##  [10,]   10  67      80700
##  [11,]   11  69      80500
##  [12,]   12  50      79200
##  [13,]   13  87      77300
##  [14,]   14  49      76000
##  [15,]   15  68      68000
##  [16,]   16  38      64400
##  [17,]   17  87      59000
##  [18,]   17  60      59000
##  [19,]   19  74      58800
##  [20,]   20  78      57600
##  [21,]   21  73      56700
##  [22,]   22  65      54400
##  [23,]   23  58      50100
##  [24,]   24  60      47200
##  [25,]   25  85      45100
##  [26,]   26  39      45000
##  [27,]   27  83      42900
##  [28,]   28  86      40100
##  [29,]   29  85      39100
##  [30,]   30  58      38900
##  [31,]   31  83      38300
##  [32,]   31  87      38300
##  [33,]   33  94      38000
##  [34,]   34  51      35300
##  [35,]   35  77      35000
##  [36,]   35  54      35000
##  [37,]   37  30      34700
##  [38,]   38  54      33400
##  [39,]   39  74      32600
##  [40,]   40  65      32100
##  [41,]   41  74      31600
##  [42,]   41  72      31600
##  [43,]   43  82      31200
##  [44,]   43  78      31200
##  [45,]   45  43      30200
##  [46,]   46  87      29700
##  [47,]   47  95      29500
##  [48,]   48  64      28500
##  [49,]   49  84      28100
##  [50,]   50  76      27800
##  [51,]   51  60      27400
##  [52,]   52  69      27000
##  [53,]   53  51      26700
##  [54,]   54  69      26600
##  [55,]   55  77      25600
##  [56,]   56  74      25500
##  [57,]   57  78      25300
##  [58,]   58  51      25200
##  [59,]   59  56      24600
##  [60,]   60  52      24400
##  [61,]   61  82      24200
##  [62,]   62  62      23700
##  [63,]   63  58      23500
##  [64,]   64  80      23400
##  [65,]   65  80      23100
##  [66,]   65  83      23100
##  [67,]   67  70      22900
##  [68,]   68  81      22600
##  [69,]   69  65      22400
##  [70,]   70  66      22100
##  [71,]   71  52      22000
##  [72,]   72  61      21600
##  [73,]   72  67      21600
##  [74,]   74  36      21200
##  [75,]   74  52      21200
##  [76,]   76  60      21100
##  [77,]   77  90      21000
##  [78,]   77  77      21000
##  [79,]   79  57      20900
##  [80,]   80  72      20500
##  [81,]   81  84      20200
##  [82,]   82  61      19600
##  [83,]   83  73      19100
##  [84,]   84  59      19000
##  [85,]   84  55      19000
##  [86,]   86  72      18900
##  [87,]   86  57      18900
##  [88,]   88  57      18700
##  [89,]   89  77      18500
##  [90,]   89  65      18500
##  [91,]   89  70      18500
##  [92,]   92  61      18000
##  [93,]   93  72      17700
##  [94,]   94  66      17500
##  [95,]   94  87      17500
##  [96,]   94  73      17500
##  [97,]   97  90      17400
##  [98,]   97  83      17400
##  [99,]   99  92      17100
## [100,]  100  70      16700
## [101,]  101  72      16500
## [102,]  101  55      16500
## [103,]  103  72      16300
## [104,]  104  75      16200
## [105,]  104  67      16200
## [106,]  106  55      16000
## [107,]  107  67      15900
## [108,]  108  83      15800
## [109,]  108  76      15800
## [110,]  112  67      15600
## [111,]  113  45      15500
## [112,]  114  68      15300
## [113,]  115  56      15200
## [114,]  116  83      14900
## [115,]  116  81      14900
## [116,]  118  78      14800
## [117,]  119  68      14700
## [118,]  120  60      14600
## [119,]  121  59      14500
## [120,]  122  69      14400
## [121,]  123  95      14300
## [122,]  124  55      14200
## [123,]  124  65      14200
## [124,]  126  93      14100
## [125,]  127  67      14000
## [126,]  128  57      13900
## [127,]  128  94      13900
## [128,]  130  78      13700
## [129,]  130  76      13700
## [130,]  130  47      13700
## [131,]  133  80      13300
## [132,]  133  80      13300
## [133,]  133  71      13300
## [134,]  136  60      13200
## [135,]  137  90      13100
## [136,]  138  64      12900
## [137,]  138  75      12900
## [138,]  140  58      12600
## [139,]  141  57      12300
## [140,]  142  50      12200
## [141,]  142  69      12200
## [142,]  144  72      12100
## [143,]  145  59      12000
## [144,]  146  99      11800
## [145,]  147  82      11600
## [146,]  148  38      11500
## [147,]  148  73      11500
## [148,]  148  73      11500
## [149,]  151  59      11400
## [150,]  151  50      11400
## [151,]  153  74      11300
## [152,]  153  69      11300
## [153,]  153  62      11300
## [154,]  153  57      11300
## [155,]  157  88      11100
## [156,]  157  72      11100
## [157,]  159  58      11000
## [158,]  159  51      11000
## [159,]  161  83      10900
## [160,]  161  85      10900
## [161,]  161  67      10900
## [162,]  164  93      10700
## [163,]  165  81      10600
## [164,]  165  73      10600
## [165,]  167  57      10500
## [166,]  167  39      10500
## [167,]  167  45      10500
## [168,]  170  70      10300
## [169,]  171  43      10200
## [170,]  171  60      10200
## [171,]  171  82      10200
## [172,]  171  64      10200
## [173,]  171  74      10200
## [174,]  171  41      10200
## [175,]  171  58      10200
## [176,]  171  74      10200
## [177,]  179  43      10100
## [178,]  179  81      10100
## [179,]  179  59      10100
## [180,]  182  85      10000
## [181,]  183  74       9900
## [182,]  184  74       9800
## [183,]  184  80       9800
## [184,]  184  51       9800
## [185,]  184  53       9800
## [186,]  184  55       9800
## [187,]  184  75       9800
## [188,]  190  63       9700
## [189,]  190  59       9700
## [190,]  190  53       9700
## [191,]  190  76       9700
## [192,]  190  64       9700
## [193,]  195  70       9600
## [194,]  195  70       9600
## [195,]  195  67       9600
## [196,]  195  58       9600
## [197,]  195  62       9600
drop(scale(new_b_data,center=center, scale=scale)%*%b_data_pca$rotation[,1])
##   [1] -6.018762029 -5.028184548 -3.373832285 -3.327800487 -3.397990109
##   [6] -3.140640833 -2.991383577 -2.955827138 -2.556421699 -2.489884525
##  [11] -2.487890549 -2.295032079 -2.519924539 -2.180073486 -2.108224408
##  [16] -1.770885226 -1.998254628 -1.789370431 -1.867910453 -1.855572101
##  [21] -1.781350867 -1.647776087 -1.470303199 -1.398602569 -1.525494151
##  [26] -1.154732220 -1.428616386 -1.367233935 -1.321376780 -1.095025704
##  [31] -1.260643206 -1.291589013 -1.313392145 -0.898714642 -1.079813780
##  [36] -0.901875390 -0.683848518 -0.823657472 -0.945429260 -0.850589131
##  [41] -0.895005134 -0.879532230 -0.921962989 -0.891017182 -0.569817245
##  [46] -0.885009058 -0.929433793 -0.651483086 -0.783581785 -0.701641564
##  [51] -0.555228001 -0.602225732 -0.442920993 -0.567291973 -0.591062884
##  [56] -0.552968378 -0.566447305 -0.342677957 -0.353566424 -0.305153738
##  [61] -0.519780411 -0.339839313 -0.291426627 -0.446743414 -0.426694807
##  [66] -0.449904162 -0.319559986 -0.384612348 -0.243362241 -0.231050086
##  [71] -0.107854610 -0.154852340 -0.201271051  0.073492712 -0.050290516
##  [76] -0.084993555 -0.302201956 -0.201628084 -0.019710474 -0.113126915
##  [81] -0.185915729  0.019816452 -0.047808905  0.075386570  0.106332377
##  [86]  0.002001273  0.118048049  0.147818351  0.010556196  0.103393617
##  [91]  0.064711358  0.184158334  0.119105972  0.182991561  0.020526075
##  [96]  0.128836399  0.036808718  0.090963880  0.053687845  0.246520118
## [101]  0.248514094  0.380033774  0.278284397  0.269960193  0.331851807
## [106]  0.454459531  0.376507261  0.267609185  0.321764347  0.445769562
## [111]  0.630856651  0.470385141  0.578107713  0.389272123  0.404745027
## [116]  0.455142957  0.547392625  0.624169391  0.646790994  0.584311628
## [121]  0.398049034  0.722392255  0.645027737  0.455595663  0.671628560
## [126]  0.763878228  0.477629514  0.631183045  0.646655948  0.871013049
## [131]  0.662947323  0.662947323  0.732575389  0.857168356  0.639959955
## [136]  0.858574580  0.773473611  0.937345321  0.965130380  1.034170693
## [141]  0.887178110  0.891157330  1.006616354  0.714625163  0.863611722
## [146]  1.218900750  0.948124939  0.948124939  1.095927262  1.165555327
## [151]  1.007069060  1.045751319  1.099906481  1.138588740  0.953135885
## [156]  1.076919113  1.212418012  1.266573174  1.046195293  1.030722390
## [161]  1.169978521  1.010904502  1.118627074  1.180518688  1.331490491
## [166]  1.470746622  1.424327912  1.272990344  1.496759692  1.365240013
## [171]  1.195038075  1.334294206  1.256929689  1.512232596  1.380712916
## [176]  1.256929689  1.597768807  1.303783641  1.473985580  1.312329833
## [181]  1.412315953  1.427201104  1.380782394  1.605139494  1.589666591
## [186]  1.574193687  1.419464653  1.588704342  1.619650149  1.666068859
## [191]  1.488130469  1.580967890  1.598648024  1.598648024  1.621857380
## [196]  1.691485445  1.660539638
predict(b_data_pca)[,1]
##   [1] -6.018762029 -5.028184548 -3.373832285 -3.327800487 -3.397990109
##   [6] -3.140640833 -2.991383577 -2.955827138 -2.556421699 -2.489884525
##  [11] -2.487890549 -2.295032079 -2.519924539 -2.180073486 -2.108224408
##  [16] -1.770885226 -1.998254628 -1.789370431 -1.867910453 -1.855572101
##  [21] -1.781350867 -1.647776087 -1.470303199 -1.398602569 -1.525494151
##  [26] -1.154732220 -1.428616386 -1.367233935 -1.321376780 -1.095025704
##  [31] -1.260643206 -1.291589013 -1.313392145 -0.898714642 -1.079813780
##  [36] -0.901875390 -0.683848518 -0.823657472 -0.945429260 -0.850589131
##  [41] -0.895005134 -0.879532230 -0.921962989 -0.891017182 -0.569817245
##  [46] -0.885009058 -0.929433793 -0.651483086 -0.783581785 -0.701641564
##  [51] -0.555228001 -0.602225732 -0.442920993 -0.567291973 -0.591062884
##  [56] -0.552968378 -0.566447305 -0.342677957 -0.353566424 -0.305153738
##  [61] -0.519780411 -0.339839313 -0.291426627 -0.446743414 -0.426694807
##  [66] -0.449904162 -0.319559986 -0.384612348 -0.243362241 -0.231050086
##  [71] -0.107854610 -0.154852340 -0.201271051  0.073492712 -0.050290516
##  [76] -0.084993555 -0.302201956 -0.201628084 -0.019710474 -0.113126915
##  [81] -0.185915729  0.019816452 -0.047808905  0.075386570  0.106332377
##  [86]  0.002001273  0.118048049  0.147818351  0.010556196  0.103393617
##  [91]  0.064711358  0.184158334  0.119105972  0.182991561  0.020526075
##  [96]  0.128836399  0.036808718  0.090963880  0.053687845  0.246520118
## [101]  0.248514094  0.380033774  0.278284397  0.269960193  0.331851807
## [106]  0.454459531  0.376507261  0.267609185  0.321764347  0.445769562
## [111]  0.630856651  0.470385141  0.578107713  0.389272123  0.404745027
## [116]  0.455142957  0.547392625  0.624169391  0.646790994  0.584311628
## [121]  0.398049034  0.722392255  0.645027737  0.455595663  0.671628560
## [126]  0.763878228  0.477629514  0.631183045  0.646655948  0.871013049
## [131]  0.662947323  0.662947323  0.732575389  0.857168356  0.639959955
## [136]  0.858574580  0.773473611  0.937345321  0.965130380  1.034170693
## [141]  0.887178110  0.891157330  1.006616354  0.714625163  0.863611722
## [146]  1.218900750  0.948124939  0.948124939  1.095927262  1.165555327
## [151]  1.007069060  1.045751319  1.099906481  1.138588740  0.953135885
## [156]  1.076919113  1.212418012  1.266573174  1.046195293  1.030722390
## [161]  1.169978521  1.010904502  1.118627074  1.180518688  1.331490491
## [166]  1.470746622  1.424327912  1.272990344  1.496759692  1.365240013
## [171]  1.195038075  1.334294206  1.256929689  1.512232596  1.380712916
## [176]  1.256929689  1.597768807  1.303783641  1.473985580  1.312329833
## [181]  1.412315953  1.427201104  1.380782394  1.605139494  1.589666591
## [186]  1.574193687  1.419464653  1.588704342  1.619650149  1.666068859
## [191]  1.488130469  1.580967890  1.598648024  1.598648024  1.621857380
## [196]  1.691485445  1.660539638
#The aboved two gives us the same thing. predict is a good function to know.
b_data$selfMade <- as.factor(b_data$selfMade)
out <- sapply(1:3, function(i){plot(b_data$selfMade,b_data_pca$x[,i],xlab=paste("PC",i,sep=""),ylab="selfMade")})

pairs(b_data_pca$x[,1:3], ylim = c(-6,4),xlim = c(-6,4),panel=function(x,y,...){text(x,y,b_data$selfMade)})

 

# Perform some visualization using PCs. (4 points)
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa

library(FactoMineR)
library(ggfortify)
library(psych)
## 
## Attaching package: 'psych'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## 
## The following object is masked from 'package:car':
## 
##     logit
library(corrplot)
## corrplot 0.92 loaded
library(devtools)
## Loading required package: usethis
# Correlation
pairs.panels(b_data[,excluded_columns],
             gap = 0,
             bg = c("red", "blue")[b_data$selfMade],
             pch=21)

pairs.panels(b_data_pca$x,
             gap=0,
             bg = c("red", "blue")[b_data$selfMade],
             pch=21)

fviz_eig(b_data_pca, addlabels = TRUE)

fviz_pca_var(b_data_pca,col.var = "cos2",
             gradient.cols = c("#FFCC00", "#CC9933", "#660033", "#330033"),
             repel = TRUE)

fviz_pca_ind(b_data_pca, col.ind = "cos2", 
                  gradient.cols = c("#FFCC00", "#CC9933", "#660033", "#330033"), 
                  repel = TRUE)

biplot(b_data_pca)

autoplot(b_data_pca,
         data = b_data[,excluded_columns],
         loadings = TRUE,
         labels = b_data$selfMade)

# Different PCA Method. ---------------------------------------------
res.pca <- PCA(b_data[,excluded_columns], graph = FALSE)
print(res.pca)
## **Results for the Principal Component Analysis (PCA)**
## The analysis was performed on 197 individuals, described by 3 variables
## *The results are available in the following objects:
## 
##    name               description                          
## 1  "$eig"             "eigenvalues"                        
## 2  "$var"             "results for the variables"          
## 3  "$var$coord"       "coord. for the variables"           
## 4  "$var$cor"         "correlations variables - dimensions"
## 5  "$var$cos2"        "cos2 for the variables"             
## 6  "$var$contrib"     "contributions of the variables"     
## 7  "$ind"             "results for the individuals"        
## 8  "$ind$coord"       "coord. for the individuals"         
## 9  "$ind$cos2"        "cos2 for the individuals"           
## 10 "$ind$contrib"     "contributions of the individuals"   
## 11 "$call"            "summary statistics"                 
## 12 "$call$centre"     "mean of the variables"              
## 13 "$call$ecart.type" "standard error of the variables"    
## 14 "$call$row.w"      "weights for the individuals"        
## 15 "$call$col.w"      "weights for the variables"
# Visualize and Interpret PCA using these functions 

#get_eigenvalue(res.pca): Extract the eigenvalues/variances of principal components
#fviz_eig(res.pca): Visualize the eigenvalues
#get_pca_ind(res.pca), get_pca_var(res.pca): Extract the results for individuals and variables, respectively.
#fviz_pca_ind(res.pca), fviz_pca_var(res.pca): Visualize the results individuals and variables, respectively.
#fviz_pca_biplot(res.pca): Make a biplot of individuals and variables.

eig.val <- get_eigenvalue(res.pca)
eig.val
##       eigenvalue variance.percent cumulative.variance.percent
## Dim.1  1.7136414        57.121381                    57.12138
## Dim.2  0.9937337        33.124456                    90.24584
## Dim.3  0.2926249         9.754163                   100.00000
fviz_eig(res.pca, addlabels = TRUE, ylim = c(0, 50))

var <- get_pca_var(res.pca)
#var$coord: coordinates of variables to create a scatter plot
#var$cos2: represents the quality of representation for variables on the factor map. It’s calculated as the squared coordinates: var.cos2 = var.coord * var.coord.
#var$contrib: contains the contributions (in percentage) of the variables to the principal components. 
#The contribution of a variable (var) to a given principal component is (in percentage) : (var.cos2 * 100) / (total cos2 of the component).
var
## Principal Component Analysis Results for variables
##  ===================================================
##   Name       Description                                    
## 1 "$coord"   "Coordinates for the variables"                
## 2 "$cor"     "Correlations between variables and dimensions"
## 3 "$cos2"    "Cos2 for the variables"                       
## 4 "$contrib" "contributions of the variables"
# Coordinates
head(var$coord)
##                 Dim.1       Dim.2      Dim.3
## rank       -0.9229106  0.03876286 0.38305808
## age         0.1387813  0.98993841 0.02759896
## finalWorth  0.9179418 -0.11069355 0.38095893
# Cos2: quality on the factore map
head(var$cos2)
##                 Dim.1       Dim.2        Dim.3
## rank       0.85176395 0.001502559 0.1467334942
## age        0.01926024 0.979978062 0.0007617025
## finalWorth 0.84261724 0.012253062 0.1451297033
# Contributions to the principal components
head(var$contrib)
##                Dim.1      Dim.2    Dim.3
## rank       49.704911  0.1512034 50.14389
## age         1.123936 98.6157639  0.26030
## finalWorth 49.171153  1.2330327 49.59581
#The plot Below is also known as variable correlation plots. It shows the relationships between all variables. It can be interpreted as follow:

#Positively correlated variables are grouped together.
#Negatively correlated variables are positioned on opposite sides of the plot origin (opposed quadrants).
#The distance between variables and the origin measures the quality of the variables on the factor map. 
#Variables that are away from the origin are well represented on the factor map.

# Correlation circle
fviz_pca_var(res.pca, col.var = "black")

# Quality of representation


corrplot(var$cos2, is.corr=FALSE)

# Total cos2 of variables on Dim.1 and Dim.2
#A high cos2 indicates a good representation of the variable on the principal component. 
#In this case the variable is positioned close to the circumference of the correlation circle.
#A low cos2 indicates that the variable is not perfectly represented by the PCs. 
#In this case the variable is close to the center of the circle.

fviz_cos2(res.pca, choice = "var", axes = 1:2)

fviz_pca_var(res.pca, col.var = "cos2",
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), 
             repel = TRUE # Avoid text overlapping
             )

# Change the transparency by cos2 values
fviz_pca_var(res.pca, alpha.var = "cos2")

corrplot(var$contrib, is.corr=FALSE)

# Contributions of variables to PC1
fviz_contrib(res.pca, choice = "var", axes = 1, top = 10)

# Contributions of variables to PC2
fviz_contrib(res.pca, choice = "var", axes = 2, top = 10)

fviz_pca_var(res.pca, col.var = "contrib",
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07")
             )

fviz_pca_var(res.pca, alpha.var = "contrib")

fviz_pca_ind(res.pca,
             geom.ind = "point", # show points only (nbut not "text")
             col.ind = b_data$selfMade, # color by groups
             palette = c("#00AFBB", "#E7B800", "#FC4E07"),
             addEllipses = TRUE, # Concentration ellipses
             legend.title = "Groups"
             )

# Description of PC

res.desc <- dimdesc(res.pca, axes = c(1,2,3), proba = 0.05)
# Description of dimension 1
res.desc$Dim.1
## 
## Link between the variable and the continuous variables (R-square)
## =================================================================================
##            correlation      p.value
## finalWorth   0.9179418 3.136636e-80
## rank        -0.9229106 9.095430e-83
res.desc$Dim.2
## 
## Link between the variable and the continuous variables (R-square)
## =================================================================================
##     correlation       p.value
## age   0.9899384 1.437337e-167
res.desc$Dim.3
## 
## Link between the variable and the continuous variables (R-square)
## =================================================================================
##            correlation      p.value
## rank         0.3830581 2.766070e-08
## finalWorth   0.3809589 3.339011e-08
# Graph of Indiviuals
ind <- get_pca_ind(res.pca)
ind
## Principal Component Analysis Results for individuals
##  ===================================================
##   Name       Description                       
## 1 "$coord"   "Coordinates for the individuals" 
## 2 "$cos2"    "Cos2 for the individuals"        
## 3 "$contrib" "contributions of the individuals"
## Principal Component Analysis Results for individuals
##  ===================================================
##   Name       Description                       
## 1 "$coord"   "Coordinates for the individuals" 
## 2 "$cos2"    "Cos2 for the individuals"        
## 3 "$contrib" "contributions of the individuals"
#To get access to the different components, use this:

# Coordinates of individuals
head(ind$coord)
##      Dim.1      Dim.2     Dim.3
## 1 6.034096 -0.4032423 3.6010705
## 2 5.040995 -1.9465043 2.7217773
## 3 3.382428 -1.0940906 1.0483848
## 4 3.336279  0.3156740 0.9497315
## 5 3.406647  1.3375850 0.9883827
## 6 3.148642 -0.4698443 0.8554672
# Quality of individuals
head(ind$cos2)
##       Dim.1       Dim.2      Dim.3
## 1 0.7349587 0.003282243 0.26175904
## 2 0.6941441 0.103497012 0.20235888
## 3 0.8328492 0.087139647 0.08001118
## 4 0.9174409 0.008213554 0.07434557
## 5 0.8075304 0.124493683 0.06797587
## 6 0.9123383 0.020315011 0.06734665
# Contributions of individuals
head(ind$contrib)
##       Dim.1      Dim.2     Dim.3
## 1 10.785451 0.08306078 22.494986
## 2  7.527424 1.93541679 12.850727
## 3  3.388995 0.61146321  1.906619
## 4  3.297149 0.05090278  1.564675
## 5  3.437702 0.91391656  1.694622
## 6  2.936706 0.11276429  1.269490
fviz_pca_ind(res.pca)

fviz_pca_ind(res.pca, col.ind = "cos2", 
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE # Avoid text overlapping (slow if many points)
             )

fviz_pca_ind(res.pca, pointsize = "cos2", 
             pointshape = 21, fill = "#E7B800",
             repel = TRUE # Avoid text overlapping (slow if many points)
             )

fviz_pca_ind(res.pca, col.ind = "cos2", pointsize = "cos2",
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE # Avoid text overlapping (slow if many points)
             )

fviz_cos2(res.pca, choice = "ind")

# Total contribution on PC1 and PC2
fviz_contrib(res.pca, choice = "ind", axes = 1:2)

# Create a random continuous variable of length 23,
# Same length as the number of active individuals in the PCA
set.seed(123)
my.cont.var <- rnorm(197)
# Color individuals by the continuous variable
fviz_pca_ind(res.pca, col.ind = my.cont.var,
             gradient.cols = c("blue", "yellow", "red"),
             legend.title = "Cont.Var")

fviz_pca_ind(res.pca,
             geom.ind = "point", # show points only (nbut not "text")
             col.ind = b_data$selfMade, # color by groups
             palette = c("#00AFBB", "#E7B800", "#FC4E07"),
             addEllipses = TRUE, # Concentration ellipses
             legend.title = "Groups"
             )

fviz_pca_ind(res.pca, geom.ind = "point", col.ind = b_data$selfMade, 
             palette = c("#00AFBB", "#E7B800", "#FC4E07"),
             addEllipses = TRUE, ellipse.type = "confidence",
             legend.title = "Groups"
             )

fviz_pca_ind(res.pca,
             label = "none", # hide individual labels
             habillage = b_data$selfMade, # color by groups
             addEllipses = TRUE, # Concentration ellipses
             palette = "jco"
             )

fviz_pca_var(res.pca, geom.var = c("point", "text"))

# Show individuals text labels only
fviz_pca_ind(res.pca, geom.ind =  "text")

# Change the size of arrows an labels
fviz_pca_var(res.pca, arrowsize = 1, labelsize = 5, 
             repel = TRUE)

# Change points size, shape and fill color
# Change labelsize
fviz_pca_ind(res.pca, 
             pointsize = 3, pointshape = 21, fill = "lightblue",
             labelsize = 5, repel = TRUE)

fviz_pca_ind(res.pca,
             geom.ind = "point", # show points only (but not "text")
             group.ind = b_data$selfMade, # color by groups
             legend.title = "Groups",
             mean.point = FALSE)

fviz_pca_ind(res.pca,
             geom.ind = "point", # show points only (but not "text")
             group.ind = b_data$selfMade, # color by groups
             legend.title = "Groups",
             mean.point = TRUE)

fviz_pca_var(res.pca, axes.linetype = "blank")

ind.p <- fviz_pca_ind(res.pca, geom = "point", col.ind = b_data$selfMade)
ggpubr::ggpar(ind.p,
              title = "Principal Component Analysis",
              subtitle = "Iris data set",
              caption = "Source: factoextra",
              xlab = "PC1", ylab = "PC2",
              legend.title = "Survivorship", legend.position = "top",
              ggtheme = theme_gray(), palette = "jco"
              )

fviz_pca_biplot(res.pca, repel = TRUE,col.ind = b_data$selfMade,
                col.var = "#2E9FDF", # Variables color
                )

fviz_pca_biplot(res.pca, 
                col.ind = b_data$selfMade, palette = "jco", 
                addEllipses = TRUE, label = "var",
                col.var = "black", repel = TRUE,
                legend.title = "Survivorship") 

fviz_pca_biplot(res.pca, 
                # Fill individuals by groups
                geom.ind = "point",
                pointshape = 21,
                pointsize = 2.5,
                fill.ind = b_data$selfMade,
                col.ind = "black",
                # Color variable by groups
                legend.title = list(fill = "Survivorship", color = "Clusters"),
                repel = TRUE        # Avoid label overplotting
             )+
  ggpubr::fill_palette("jco")+      # Indiviual fill color
  ggpubr::color_palette("npg")      # Variable colors

fviz_pca_biplot(res.pca, 
                # Individuals
                geom.ind = "point",
                fill.ind = b_data$selfMade, col.ind = "black",
                pointshape = 21, pointsize = 2,
                palette = "jco",
                addEllipses = TRUE,
                # Variables
                alpha.var ="contrib", col.var = "contrib",
                gradient.cols = "RdYlBu",
                
                legend.title = list(fill = "selfMade", color = "Contrib",
                                    alpha = "Contrib")
                )